Question: In python [20 pts) Write the method translate translation Dict, txt) that takes in a dictionary and a string. The dictionary contains keys of strings
In python
[20 pts) Write the method translate translation Dict, txt) that takes in a dictionary and a string. The dictionary contains keys of strings that have a value of another string. You will be translating the input string using the key-value pairs in the dictionary to a new string that has replaced all the words in the input string with the words' value in the dictionary. Your function should convert txt to all lowercase letters and remove punctuation before making the translations. If a word in the string is not in the translation dictionary, the word will retain its original form. You can assume that all the keys in the dictionary are lowercase strings. Hint: str.lower() method returns a new string with all lowercase letters, str.split() splits a string into a list, default separator is any whitespace, and str.isalnum() returns True if all characters in a string are alphanumeric. Example: >>> myDict = {'up': 'down', 'down': 'up', 'left': 'right', 'right': 'left', '1': 'one' >>> text = 'UP down left right forward 1 time >>> translate(myDict, text) 'down up right left forward one time
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
