Created
November 8, 2012 23:17
-
-
Save saltavenger/4042549 to your computer and use it in GitHub Desktop.
crypt2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Problem 2: Decryption | |
# | |
def findBestShift(wordList, text): | |
""" | |
Finds a shift key that can decrypt the encoded text. | |
text: string | |
returns: 0 <= int < 26 | |
""" | |
#1. Set the maximum number of real words found to 0. | |
realWords = 0 | |
#2. Set the best shift to 0. | |
bestShift = 0 | |
count = 0 | |
validWords = 0 | |
#3. For each possible shift from 0 to 26: | |
for i in range(0, 26): | |
#4. Shift the entire text by this shift. | |
applyShift(text, bestShift) | |
#5. Split the text up into a list of the individual words. | |
newText = text.split(' ') | |
#6. Count the number of valid words in this list. | |
if isWord(wordList, newText[count]) == True: | |
validWords += 1 | |
#7. If this number of valid words is more than the largest number of real words found, then: | |
if validWords > realWords: | |
#9. Set the best shift to the current shift. | |
bestShift = shift | |
#10. Increment the current possible shift by 1. Repeat the loop starting at line 3. | |
else: | |
bestShift += 1 | |
#11. Return the best shift. | |
return bestShift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment