Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python. For loops only. Return ONLY, NO prints. Create a function called translate which expects two arguments. The first argument is a string representing a

Python. For loops only. Return ONLY, NO prints.

Create a function called translate 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 Morse Code equivalent. Each two-element list in the key represents an alphabetic character that might be found in the original message paired with the Morse Code pattern of dots and dashes 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 translate function into smaller functions. Writing translate 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.

key = [["a",".-"],["b","-..."],["c","-.-."],["d","-.."], ["e","."],["f","..-."],["g","--."],["h","...."], ["i",".."],["j",".---"],["k","-.-"],["l",".-.."], ["m","--"],["n","-."],["o","---"],["p",".--."], ["q","--.-"],["r",".-."],["s","..."],["t","-"], ["u","..-"],["v","...-"],["w",".--"],["x","-..-"], ["y","-.--"],["z","--.."]]

test1 = "the quick brown fox jumped over the lazy dog" test2 = "i do not like green eggs and ham" test3 = "help! i've fallen and I can't get up" test4 = ""

>>> translate(test1, key) '- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- - ..- .---..---.--..-.. ---...-..-. -..... .-...- --.. -.-- -.. --- --. '

>>> translate(test2, key) '.. -..--- -.---- .-....-.-. --..-...-. .--. --.... .--.-.. .....---'

>>> translate(test3, key) '.... . .-.. .--. *** .. *** ...- . ..-. .- .-.. .-.. . -. .- -.-.. *** -.-..--.***- --..- ..-.--.'

>>> translate(test4, key) ''

As in the examples above, the returned string should have exactly one space between the Morse code patterns within a single word (i.e., "the" becomes '- .... .') and exactly three spaces between words

(i.e., "the lazy" becomes '- .... . .-.. .- --.. -.--') .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genetic Databases

Authors: Martin J. Bishop

1st Edition

0121016250, 978-0121016258

More Books

Students also viewed these Databases questions

Question

4. What decision would you make and why?

Answered: 1 week ago

Question

3. Review the evidence. Do you believe the testimony presented?

Answered: 1 week ago

Question

1. What are the marketing implications of this situation?

Answered: 1 week ago