Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

请问这个要怎么弄,没看懂要求

要求及代码:

# 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):
    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.')

例子:

无效的:
$ 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Prompts the user for an arity (a natural number) n and a word.

提示用户输入层数(一个自然数) n 和一个字符串 w

Call symbol a word consisting of nothing but alphabetic characters
and underscores.

我们称仅由字母和下划线组成的字符串是合法的 word

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.

合法的 word 可以嵌套 n 层,:

  • 0 层:word
  • 1 层:word(word, word, word, ...),括号内的 word 两边都可以有空格
  • 2 层:word(word(word, word, ...), word(word, ...), ...)

以此类推

最后需要你判断 w 是否为合法的 word 并输出。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...