Question: WRITE IN PYTHON LANGUAGE A secret decoder ring is a device that can map letters of the alphabet to numbers in order to send secret
WRITE IN PYTHON LANGUAGE
A "secret decoder ring" is a device that can map letters of the alphabet to numbers in order to send "secret" messages. You will use the supplied function create_code_ring that expects an integer value as a "seed" to return a dictionary that maps the alphabet A-Z, hyphen, period, and blank, each to a different number from 1 to 29. It uses an auxiliary function relative_prime, so copy that, too.
Using that supplied function, define the functions encode_message and decode_message to work as follows:
encode_message will expect a string to be encoded, and a seed number. It will return a list of int values, the numbers that correspond to the letters in the string, as found in the code ring dictionary. For example, the string "Hello World" and the seed number 12345 should result in:
>>> encode_message("Hello World", 12345)
[15, 13, 8, 8, 10, 29, 25, 10, 12, 8, 22]
This is because the code ring dictionary generated by create_code_ring maps H to 15, E to 13, L to 8, and so on. Lower-case letters should map to the number for their upper-case equivalent. For a string containing characters not in the dictionary, it should map them to 99999, and also print a message to the screen saying it didn't find the character in the dictionary. For example:
>>> encode_message("Hello, World!", 12345)
Character , not encoded, set to 99999
Character ! not encoded, set to 99999
[15, 13, 8, 8, 10, 99999, 29, 25, 10, 12, 8, 22, 99999]
When encode_message is tested and working correctly, then define a function decode_message that takes an encoded message (in the form of a list of int values) and the seed number used to encode the message, and returns the decoded message as a string. For example, as above:
>>> code = encode_message("Hello World", 12345)
>>> decode_message(code, 12345)
'HELLO WORLD'
>>> decode_message(code, 6789)
'ENVVM RMDVQ'
Note how the correct seed value is needed, otherwise it (very likely) won't decode correctly! If your coded message contains a 99999, your decode_message function should insert an @ like this:
>>> code = encode_message("Hello, World!", 12345)
Character , not encoded, set to 99999
Character ! not encoded, set to 99999
>>> decode_message(code, 12345)
'HELLO@ WORLD@'
HINT: for decode_message, you will need an inverse dictionary from the one you get from create_code_ring. An inverse dictionary is where each item has its key and value swapped the key becomes the value, and the value becomes the key. You can make your own very quickly in one statement for example, a dictionary named original_dict can become an inverse dictionary with: inverse_dict = {a_value: a_key for a_key, a_value in original_dict.items()}
And, because this problem was inspired by the movie "A Christmas Story," be sure to drink your Ovaltine!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
