Answered step by step
Verified Expert Solution
Question
1 Approved Answer
pls use Python to do this Problem Multi-level code-breaking Your friend has sent you another message, but this one cant be decrypted by your solution
pls use Python to do this Problem Multi-level code-breaking Your friend has sent you another message, but this one cant be decrypted by your solution to Problem 2 it must be using a multi-layer shift. To keep things from getting too complicated, we will add the restriction that a shift can begin only at the start of a word. This means that once you have found the correct shift at one location, it is guaranteed to remain correct at least until the next occurrence of a space character. Implement the following function according to the specification. def find_best_shifts(wordlist, text): """ Given a scrambled string, returns a shift key that will decode the text to words in wordlist, or None if there is no such key. Hint: Make use of the recursive function find_best_shifts_rec(wordlist, text, start) wordlist: list of words text: scambled text to try to find the words for returns: list of tuples. each tuple is (position in text, amount of shift) Examples: >>> s = random_scrambled(wordlist, 3) >>> s 'eqorqukvqtbmultiform wyy ion' >>> shifts = find_best_shifts(wordlist, s) >>> shifts [(0, 25), (11, 2), (21, 5)] >>> apply_shifts(s, shifts) 'compositor multiform accents' >>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)]) >>> s 'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?' >>> shifts = find_best_shifts(wordlist, s) >>> print(apply_shifts(s, shifts)) Do Androids Dream of Electric Sheep? """ To solve this problem successfully, we highly recommend that you use recursion (did we say use recursion again?). The non-recursive version of this function is much more difficult to understand and code. The key to getting the recursion correct is in understanding the seemingly unnecessary parameter start. As always with recursion, you should begin by thinking about your base case, the simplest possible sub-problem you will need to solve. What value of start would make a good base case? (Hint: the answer is NOT zero.) To help you test your code, weve given you two simple helper functions: random_string(wordlist, n) generates n random words from wordlist and returns them in a string. random_scrambled(wordlist, n) generates n random words from wordlist and returns them in a string after encrypting them with a random multi-level Caesar shift. You can start by making sure your code decrypts a single word correctly, then move up to 2 and higher. NOTE: This function depends on your implementation of apply_shifts, so it will not work correctly until you have completed Problem 3. def find_best_shifts_rec(wordlist, text, start): """ Given a scrambled string and a starting position from which to decode, returns a shift key that will decode the text to words in wordlist, or None if there is no such key. Hint: You will find this function much easier to implement if you use recursion. wordlist: list of words text: scambled text to try to find the words for start: where to start looking at shifts returns: list of tuples. each tuple is (position in text, amount of shift) """ ### TODO.
Problem Multi-level code-breaking
Your friend has sent you another message, but this one cant be decrypted by your solution to Problem 2 it must be using a multi-layer shift.
To keep things from getting too complicated, we will add the restriction that a shift can begin only at the start of a word. This means that once you have found the correct shift at one location, it is guaranteed to remain correct at least until the next occurrence of a space character.
Implement the following function according to the specification.
def find_best_shifts(wordlist, text):
"""
Given a scrambled string, returns a shift key that will decode the text to
words in wordlist, or None if there is no such key.
Hint: Make use of the recursive function
find_best_shifts_rec(wordlist, text, start)
wordlist: list of words
text: scambled text to try to find the words for
returns: list of tuples. each tuple is (position in text, amount of shift)
Examples:
>>> s = random_scrambled(wordlist, 3)
>>> s
'eqorqukvqtbmultiform wyy ion'
>>> shifts = find_best_shifts(wordlist, s)
>>> shifts
[(0, 25), (11, 2), (21, 5)]
>>> apply_shifts(s, shifts)
'compositor multiform accents'
>>> s = apply_shifts("Do Androids Dream of Electric Sheep?", [(0,6), (3, 18), (12, 16)])
>>> s
'JufYkaolfapxQdrnzmasmRyrpfdvpmEurrb?'
>>> shifts = find_best_shifts(wordlist, s)
>>> print(apply_shifts(s, shifts))
Do Androids Dream of Electric Sheep?
"""
To solve this problem successfully, we highly recommend that you use recursion (did we say use recursion again?). The non-recursive version of this function is much more difficult to understand and code. The key to getting the recursion correct is in understanding the seemingly unnecessary parameter start. As always with recursion, you should begin by thinking about your base case, the simplest possible sub-problem you will need to solve. What value of start would make a good base case? (Hint: the answer is NOT zero.)
To help you test your code, weve given you two simple helper functions: random_string(wordlist, n) generates n random words from wordlist and returns them in a string.
random_scrambled(wordlist, n) generates n random words from wordlist and returns them in a string after encrypting them with a random multi-level Caesar shift. You can start by making sure your code decrypts a single word correctly, then move up to 2 and higher.
NOTE: This function depends on your implementation of apply_shifts, so it will not work correctly until you have completed Problem 3.
def find_best_shifts_rec(wordlist, text, start):
"""
Given a scrambled string and a starting position from which
to decode, returns a shift key that will decode the text to
words in wordlist, or None if there is no such key.
Hint: You will find this function much easier to implement
if you use recursion.
wordlist: list of words
text: scambled text to try to find the words for
start: where to start looking at shifts
returns: list of tuples. each tuple is (position in text, amount of shift)
"""
### TODO.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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