Question: Please use for/while/if statements Create a function called encoder which expects two arguments. The first argument is a string representing a message to be translated

Please use for/while/if statements

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. (Hint: you could simplify things by decomposing the encoder function into smaller functions. Writing encoder as one big function could get complicated.)

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 jumped over the lazy dog"

>>> 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 gur ynml qbt'

>>> 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!