美文网首页
2020-10-13

2020-10-13

作者: larwas | 来源:发表于2020-10-13 11:33 被阅读0次

    ```PYTHON

    # DO *NOT* WRITE YOUR NAME TO MAINTAIN ANONYMITY FOR PLAGIARISM DETECTION

    # Prompts the user for an arity (a natural number) n and a word.

    # Call symbol a word consisting of nothing but alphabetic characters

    # and underscores.

    # Checks that the word is valid, in that it satisfies the following

    # inductive definition:

    # - a symbol, with spaces allowed at both ends, is a valid word;

    # - a word of the form s(w_1,...,w_n) with s denoting a symbol and

    #  w_1, ..., w_n denoting valid words, with spaces allowed at both ends and

    #  around parentheses and commas, is a valid word.

    import sys

    def is_valid(word,arity):

    high =0

        result = []

    tmp = []

    for cin word:

    if c ==')':

    high -=1

                if high ==arity -1:

    result.append(''.join(tmp))

    tmp = []

    if high ==arity:

    if c ==',':

    result.append(''.join(tmp))

    tmp = []

    else:

    tmp.append(c)

    elif high >arity:

    tmp.append(c)

    if c =='(':

    high +=1

        if tmp:

    result.append(''.join(tmp))

    return result

    return False

        # REPLACE THE RETURN STATEMENT ABOVE WITH YOUR CODE

    try:

    arity =int(input('Input an arity : '))

    if arity <0:

    raise ValueError

    except ValueError:

    print('Incorrect arity, giving up...')

    sys.exit()

    word =input('Input a word: ')

    if is_valid(word, arity):

    print('The word is valid.')

    else:

    print('The word is invalid.')

    ```

    例子:

    QUIZ 3

    COMP9021 PRINCIPLES OF PROGRAMMING

    $ python3 quiz_3.py

    Input an arity : 0

    Input a word: f_1

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 0

    Input a word: ()

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 0

    Input a word: function_of_arity_one(hello)

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 1

    Input a word: f)

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 1

    Input a word: f[a]

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 2

    Input a word: f(a, g(b))

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: constant

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: f((a,b,c))

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: f(g(a,a), f(a,b))

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: f(g(a,b,c),g(a,b,c),g(a,b,c)

    The word is invalid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: f(a, g(a, b, f(a,b,c)), b, c)

    The word is invalid.

    Date: Term 3, 2020.

    2 COMP9021 PRINCIPLES OF PROGRAMMING

    $ python3 quiz_3.py

    Input an arity : 0

    Input a word: a

    The word is valid.

    $ python3 quiz_3.py

    Input an arity : 1

    Input a word: function_of_arity_one(hello)

    The word is valid.

    $ python3 quiz_3.py

    Input an arity : 2

    Input a word: F(g(a,a), f(a,b))

    The word is valid.

    $ python3 quiz_3.py

    Input an arity : 3

    Input a word: ff(ff(ff(a,b,ff(aa,bb,cc)) , b , ff(a,b,c)) , b , ff(a,ff(a,b,c),c))

    The word is valid.

    $ python3 quiz_3.py

    Input an arity : 4

    Input a word: f(a, FF(a, b, fff(a, b, c, FfFf(a,b,c,d)), FfFf(a,b,c,d)), c,d)

    The word is valid.

    相关文章

      网友评论

          本文标题:2020-10-13

          本文链接:https://www.haomeiwen.com/subject/cicvpktx.html