In: Computer Science
magine a table is given as following. This table is called lookup table. The table works as following. It assigns a number to some uppercase or lowercase alphabents as shown in the table.
A B F M N P Q W Z
letter
number
letter
Number
12
e
8
10
f
3
5
i
2
4
m
0
0
n
1
11
p
2
10
q
3
9
r
4
9
s
5
1- Design and implement a function with 2 parameters: 1- a string (a word) 2- a variable that represents the above lookup table. The function converts the string to a number and returns it. This function replaces each character in the string with its corresponding number from the above table. If the character does not exist in the above table, the function replaces it with 0. The function will eventually return the number.
2- Define a main function and call the method you designed in 1. When calling the above function in the main method, pass to the function the word “We have missed Vancouver’s summer” and the above lookup table. What is the output of the function for the this word, “We have missed Vancouver’s summer” ?
python code
# Code for part 1
def conversion(string,table):
#the given word is stored in variable string and the table is stored in variable table
s="" # An empty string is declared to store the resultant string
for ele in string: # iterates through every character in the string
if ele in table.keys(): # if the character is present as the key in the dictionary in which we store the table
s=s+str(table[ele]) # then the values is converted to string and concatenated to resultant string
else:# if the character is not present
s=s+'0' # then "0" is concatenated
return int(s) # resultant string after conversion is converted to number and is returned
# Code for part 2
def main():
# call to conversion function and the word is passed as first argument,the table is stored as dictionary and passed as second argument
s=conversion("We have missed Vancouver's summer",{'A' : 12 ,'B' : 10,'F' :5 ,'M' :4 ,'N' :0 ,'P' :11 ,'Q' :10 ,'W' :9 ,'Z' :9,
'e':8 ,'f' : 3,'i' :2 ,'m' :0,'n' :1 ,'p' :2 ,'q' :3 ,'r' :4 ,'s' :5 })
print(s) # prints the number we obtained after conversion
if __name__ == "__main__":
main() # call to main function
OUTPUT:
Please comment if you have any doubts.
Rate Please!!!!!!!