Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions cyphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ def letter_frequency(c):
0.15, 1.974, 0.074]
return frequencies[ord(c) - ord('a')]

# This is what map() is all about.
# 1. Take a list of letters, aka some_list_of_characters
# 2. For each letter, compute the letter_frequency for that letter
# In other words, "map" the letter to its letter frequency score
# 3. The result of the mapping is a list of floats. Specifically, it
# is a list of the letter frequency scores that correspond to the
# characters in some_list_of_characters. Sum up those floats to
# get the frequency score for the entire string.
#
# sum of,
# the mapping to scores of,
# the list of letters
#
# It's just composite functions in algebra/calculus all over again :-)
def frequency_score(some_list_of_characters):
return sum(map(letter_frequency, some_list_of_characters))

if mode == 'e':
message = input('What message do you want to send? ')
shift = int(input('How much do you want to shift the letters? '))
Expand All @@ -41,18 +58,23 @@ def letter_frequency(c):
elif mode == 'd':
message1 = input('What is the message you want to decrypt? ')
listmessage1 = list(message1)
freqall = []
for i in range(26):
shift = i
decrypt = new_message(listmessage1, encription, shift)
frequency = []
(map(letter_frequency, decrypt))
for i in range(len(decrypt)):
frequency.append(letter_frequency(decrypt[i]))
freqall.append(sum(frequency))
for x in freqall:
if x == max(freqall):
print(''.join(decrypt))

decrypted_strings = []
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First: generate the "decrypted" list of characters for each of the 26 possible shift values. We will use this later.

for shift in range(26):
# Each entry in the decrypted_strings list is itself a list
# of (decrypted) characters.
decrypted_strings.append(new_message(listmessage1, encription, shift))

best_score = 0
best_score_shift = 0

for shift in range(26):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, for each entry in the list of possible decrypted strings, compute the frequency_score for that string.

Each step along the way, if the frequency score that we are looking at now is better (higher) than any score we have seen so far, then it is the "best" frequency score and we want to record it as such. We need to record it so that we can know whether the next or the next or the next decrypted string is "better" or not.

Also, we keep track of the "shift" value associated with the best score, so that we can actually pluck that decrypted string out of the decrypted_strings list at the end of the exercise. Alternatively we could take your approach like this:

    for x in decrypted_strings:
        if frequency_score(x) == best_score:
            print(''.join(x))

But that seemed clunkier to me than simply recording the list index of the best-scoring string, at the moment when we discover that it is the best-scoring string.

this_score = frequency_score(decrypted_strings[shift])
if this_score > best_score:
best_score = this_score
best_score_shift = shift

print(''.join(decrypted_strings[best_score_shift]))

brutethisbish = input('This decryption may not be correct, would you like to see the list of all possible decryptions?(y/n) ')
for shift in range(26):
Expand Down