Answered step by step
Verified Expert Solution
Question
1 Approved Answer
decode_string(encoded_string, You will now write a function encoder) that takes an encoded string (see definition in question 6) and an encoder (of type dict,
decode_string(encoded_string, You will now write a function encoder) that takes an encoded string (see definition in question 6) and an encoder (of type dict, see question 5) as input, and returns a decoded string that was used to generate encoded_string. In other words, it must return a string such that calling encode_string(string, encoder) will return encoded_string. If there is more than one solution, it is OK to return one. You can view encoder as an output of a call to create_encoder (some_string) in question 5, and encoded_string as an output of a call to encode_string(some_string, encoder). But this question will be marked independently of questions 5 and 6, even if you did not solve question 5 or 6. For example, decode_string('02010201', {'a' : '01', 'b': '02', 'c': '03' }) should return 'baba'. decode_string('A 0201 0301t', {'a': '01', 'b': '02', 'c' : '03' }) should return 'A bad cat' decode_string('ABC 040506 ghijklmnopqrstuvwxyz', {'a': '01', 'b': '02', 'c': '03', 'd':'04', 'e': '05', 'f': '06'}) should return 'ABC def ghijklmnopqrstuvwxyz'. Requirements: The function that you write must be named decode_string and it must have two parameters. You can assume that the first argument is a string. You can assume that the second argument is a dictionary as defined in question 5. That means its keys are single non-numeric character and values are 2-digit number string. The function must return a string. A skeleton code file named decode_string.py is provided. You must write your solution into this file. Remember that: You must upload the python file with your solution using the right submission link for question 7 on wattle, before the end of the exam. You can re-upload it as many times as you wish during the exam, but we will mark this question based on the contents of the last uploaded file. The file must contain only syntactically correct python code (and comments). Do not import any module that you do not use. You must define a function named decode_string that has two parameters. You may also define additional functions if it helps you break down or solve the problem. def decode_string(encoded_string, encoder): def test_decode_string(): This function runs a number of tests of the decode_string function. If it works ok, you will just see the output ("all tests passed") at the end when you call this function; if some test fails, there will be an error message. assert decode_string('02010201 {'a : '01 'b' : '02 assert decode_string('A 0201d 0301t', {'a' : '01', 'b' : assert (decode_string('ABC 040506 ghijklmnopqrstuvwxyz', == { 'a':'01', 'b':'02','c':'03','d': '04' 'e': '05 'ABC def ghijklmnopqrstuvwxyz') 'abcd' assert decode_string('', {'a' : 01 'b' : '02', 'c': '03' }) assert decode_string('abcd', {}) assert decode_string('', {}) print("all tests passed") == : '03' }) '02' 'c' : '03' }) 3 ) 2 ) == 'baba' 'f': '06'}) == 'A bad cat'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Certainly Heres an implementation of the decodestring function based on the requirements python def ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started