Home > other >  How to do excuse me this topic?
How to do excuse me this topic?

Time:10-13

Completion of def is_valid function
` ` ` 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 the 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 charges at both ends, is a valid word.

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

# w_1,... , w_n denoting valid words, with Spaces charges at both ends and

# around parentheses and commas, is a valid word.

The 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 & lt; 0:

Raise ValueError

Except, ValueError:

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

Sys. The exit ()

Word=input (' input a word: ')

If is_valid (word, arity) :

Print (' The word is valid.)

The else:

Print (' The word is invalid.)

` ` `

Example:

Quizzes 3

COMP9021 all 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 all 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, and FfFf (a, b, c, d)), FfFf (a, b, c, d)), c, d)

The word is valid.
  • Related