-
Notifications
You must be signed in to change notification settings - Fork 0
In which dad attempts to explain what the teacher was going on about when discussing map #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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? ')) | ||
|
|
@@ -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 = [] | ||
| 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): | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 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): | ||
|
|
||
There was a problem hiding this comment.
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.