Python: need to create a program that decodes caesar ciphers and meets the follo
ID: 3795614 • Letter: P
Question
Python: need to create a program that decodes caesar ciphers and meets the following requirements
1. Input the cipher-text.
2. Find the most common character.
3. Find the shift from “E” to that most common character.
4. Use the shift to decode each character of the cipher-text and print the resulting plain-text.
6. If the plain-text isn’t readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable.
7. uses get_char, get_shift, output_plaintext functions
Explanation / Answer
print "Cracking a Caesar cypher."
cipher = raw_input("Input cipherText: ")
#cipher = "Wlv d frpprq surri, Wkdw orzolqhvv lv brxqj dpelwlrq'v odgghu"
cipher = cipher.upper()
def get_char_dict(s):
dict_char = {}
for c in s:
if c.isalpha():
if c in dict_char:
dict_char[c] += 1
else:
dict_char[c] = 1
return dict_char
def find_max_key(dict_char):
max_value = -1
max_char = ''
for key in dict_char:
if dict_char[key] > max_value:
max_value = dict_char[key]
max_char = key
dict_char[max_char] = 0
return (max_char, dict_char)
def find_shift_from_E(c):
return (ord(c) - ord('E'))%26
dict_char = get_char_dict(cipher)
def create_shift_dict(shift):
shift_dict = {}
for i in range(0, 26):
shift_from_A = ord('A') + (i+shift)%26
shift_dict[chr(shift_from_A)] = chr(ord('A') + i)
return shift_dict
response = "no"
while response != "yes":
(max_char, dict_char) = find_max_key(dict_char)
shift = find_shift_from_E(max_char)
shift_dict = create_shift_dict(shift)
result = ""
for c in cipher:
if c.isalpha():
result += shift_dict[c]
else:
result += c
print result
response = raw_input("Is the plaintext readable as English? (yes/no): ")
pastebin link of code: http://pastebin.com/UmkvXD39
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.