Question
# Returns True if the parameter string is a legitimate code, the function returns # False otherwise - 3 marks #-------------------------------------------------- Define the is_legitimate_code()
# Returns True if the parameter string is a legitimate code, the function returns # False otherwise - 3 marks #--------------------------------------------------
""" Define the is_legitimate_code() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a legitimate code or not. A legitimate code is a string made up of one letter followed by one or more digits (can also include spaces before, between or after the digits). The first three lines of code inside the function should be:
code_letters = ["S", "B", "N", "T", "P"]
min_for_each_letter = [1, 3, 4, 0, 3] #inclusive
max_for_each_letter = [7, 9, 6, 7, 5]#inclusive
where: code_letters is the list of code letters which are legitimate for the first letter of the code string,
min_for_each_letter is a list which contains the minimum number (inclusive) for each digit following that letter,
max_for_each_letter is a list which contains the maximum number (inclusive) for each digit following that letter.
For example, the third element of the code_letters list is the letter 'N', the corresponding third element of the min_for_each_letter list is 4 and the corresponding third element of the max_for_each_letter list is 6. This indicates that the code digits which follow the letter 'N' can be any number made up of the digits 4, 5 or 6. The number part of a legitimate code string can also contain any number of spaces. Note: The number part of a parameter code string to be tested could contain an alphabetic character thus making the code not legitimate. You will find it useful to use the isdigit() method which returns True if a string is a digit, False otherwise.
For example, the following code:
print("1.", is_legitimate_code('B747346'))
print("2.", is_legitimate_code('N 444 454'))
print("3.", is_legitimate_code('T 400 4854'))
print("4.", is_legitimate_code('S 444S454'))
print("5.", is_legitimate_code('P '))
print("6.", is_legitimate_code('T 0 '))
prints: 1. True 2. True 3. False 4. False 5. False 6. True """
def is_legitimate_code(code_str):
code_letters = ["S", "B", "N", "T", "P"]
min_for_each_letter = [1, 3, 4, 0, 3] #inclusive
max_for_each_letter = [7, 9, 6, 7, 5] #inclusive
return True
def test_is_legitimate_code():
print("1.", is_legitimate_code('B747346'))
print("2.", is_legitimate_code('N 444 454'))
print("3.", is_legitimate_code('T 400 4854'))
print("4.", is_legitimate_code('S 444S454'))
print("5.", is_legitimate_code('P '))
print("6.", is_legitimate_code('T 0 '))
codes_to_check = ['B45 5', "P-534", 'S5 45 4 4 54', 'B3 9S56 8 ']
number = 7
for code in codes_to_check:
print(str(number) + ". ", code, ": ", is_legitimate_code(code), sep = "")
number = number +1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started