Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. 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
2. 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 Bzdrz rzkzc.') "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, it's 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. Hints/reminders: - Since deciphering involves rotating the characters, you already have the function needed to create each of the possible decipherings! We recommend that you start by using a list comprehension to create a list of all possible decipherings. You will consider all possible shifts from 0 to 25-that could be used to create a deciphering. Assign the result of this list comprehension to a variable. Next, you should use the "list-of-lists" technique that we discussed in lecture to give each possible deciphering a score. The best_word function from lecture is a good example of this technique. As discussed above, the score that you assign to a given deciphering should provide some measure of its "Englishness." You are welcome to write additional helper functions to assist you in the scoring and in the other steps involved. After you have scored all of the options, you should choose the option with the highest score. Here again, the best word function from lecture is a good model for this. Once you think you have a working decipher, try it on the following string: kwvozibctibqwva izm qv wzlmz nwz iv mfkmttmvb rwj def letter_prob(c): """ if c is the space character C or an alphabetic character, returns c's monogram probability (for English); returns 1.0 for any other character. adapted from http://www.cs.chalmers.se/Cs/Grundutb/Kurser/krypto/en_stat.html 1 # check to ensure that c is a single character assert(type(c)-str and len(c)-1) if c'': return 0.1904 if c =-' e' or c =-'E' : return 0.1017 if c't' or c'T' : return 0.0737 if c __ 'a' or c-"A' : return 0.0661 if c =- 'o' or c-'0': return 0.0610 if c _ 'i' or c =-'1' : return 0.0562 if c _ 'n' or c=': if c =- 'h' or c -- 'H': return 0.0542 if c =-'s' or c =_ 'S': return 0.0508 if c _ 'r' or c =_ 'R': return 0.0458 if c-' d' or c -- "D': return 0.0369 if c'1' or c'L' : return 0.0325 if c =- ' u' or c =_ 'U': return 0.0228 if c-"m' or c -- "M": return 0.0205 if c =-' c' or c =-"c": return 0.0192 if c =- 'w' or c =- 'W' : return 0.0190 if c 'f' or c 'F': return 0.0175 - 'N': return 0.0557 == 'y' or c == 'Y': if c _ 'g' or c-"G': return 0.0161 'p' or c == 'P': retu 'b' or c =- 'B': return 0.0115 c if c'v' or c'V' : return 0.0088 or c-' K' : return 0.0066 if c =-'x' or c =-'X' : return 0.0014 j' or c- "J': return 0.0008 if c'q' or c--'Q': return 0.0008 if c =- 'z' or c -- 'Z' : return 0.0005 return 1.0
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