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

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

要求及代码:

# 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


回答:

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 并输出。


回答:

请问你也学9021吗

以上是 请问这个要怎么弄,没看懂要求 的全部内容, 来源链接: utcz.com/a/164614.html

回到顶部