Question
Python Write a function decipher(s) that takes as input an arbitrary string s that has already been enciphered by having its characters rotated by some
Python
Write a function decipher(s) that takes as input an arbitrary string s that has already been enciphered by having its characters rotated by some amount (possibly 0). decipher should return, to the best of its ability, the original English string, which will be some rotation (possibly 0) of the input string s. For example:
>>> decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc.') result: 'Caesar cipher? I prefer Caesar salad.'
Note that decipher does not take a number specifying the amount of rotation! Rather, it should determine the rotation (out of all possible rotations) that produces the most plausible English string. We have given you a helper function called letter_prob(c) that takes a single-character string c and returns a value that is based on the frequency with which that character appears in English texts. That function provides the basis for a number of possible approaches for judging the Englishness of a given rotation, but you are welcome to try an alternative approach. Use a short comment above your decipher function to describe your overall approach.
Your function does not need to be perfect. Some strings have more than one English deciphering. In addition, its difficult if not impossible to handle very short strings correctly. However, your function should work almost all of the time on long stretches of English text (e.g., sentences of 8 or more words). You will not lose any credit for not getting the correct deciphering of a single word or short phrase.
Here are two more examples:
>>> decipher('Hu lkbjhapvu pz doha ylthpuz hmaly dl mvynla lclyfaopun dl ohcl slhyulk.') result: 'An education is what remains after we forget everything we have learned.' >>> decipher('python') result: 'eniwdc'
Note that our version of decipher gets the second example wrong! (When you provide English text as the input, you should ideally get back the text itselfi.e., a string that has been deciphered using a rotation of 0but that didnt happen in this case, which is completely okay!) Its possible that your version of decipher will return a different value here. It might even return 'python', but its okay if it doesnt.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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