Question: Create a function called encoder which expects two arguments. The first argument is a string representing a message to be translated into a coded message.
Create a function called encoder which expects two arguments. The first argument is a string representing a message to be translated into a coded message. The second argument is a list of lists serving as the key to be used to convert the original string to its coded equivalent. Each two-element list in the key represents a character that might be found in the original message paired with the character that will be substituted for the original character while building the coded message. The function returns a new string created by replacing every character in the message with its counterpart. This function does not print anything.
Usually the string passed as the first argument (the message) will include only the characters found as the first elements of the lists within the list that is passed as the second argument (the key). But if something other than one of those characters is found in the message, your function should translate the unexpected character into "?" as shown in the example using test3 below.
>>> code1 = [['a', 'n'],['b', 'o'],['c', 'p'],['d', 'q'], ['e', 'r'],['f', 's'],['g', 't'],['h', 'u'], ['i', 'v'],['j', 'w'],['k', 'x'],['l', 'y'], ['m', 'z'],['n', 'a'],['o', 'b'],['p', 'c'], ['q', 'd'],['r', 'e'],['s', 'f'],['t', 'g'], ['u', 'h'],['v', 'i'],['w', 'j'],['x', 'k'],
['y', 'l'],['z', 'm'],[' ', ' ']] >>> code2 = [['1','!'], ['2','@'],['3','#'],['4','$'],['5','%'],['6','^'], ['7','&'],['8','*'],['9','('],['0',')']]
>>> test1 = "the quick brown fox >>> test2 = "i do not like green >>> test3 = "we can't stop!"
>>> test2 = "i do not like green eggs and ham"
>>> test3 = "we can't stop!" >>> test4 = ""
>>> test5 = "9876543210"
>>> encoder(test1, code1) 'gur dhvpx oebja sbk whzcrq bire
>>> x = encoder(test2, code1) >>> print(x) v qb abg yvxr terra rttf naq unz >>> y = encoder(x, code1) >>> print(y) i do not like green eggs and ham
>>> encoder(test3, code1)
'jr pna?g fgbc?'
>>> encoder(test4, code1)
''
>>> encoder(test5, code2)
'(*&^%$#@!)'
'
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
