Created
November 9, 2012 00:14
-
-
Save mooreniemi/4042827 to your computer and use it in GitHub Desktop.
600x PS5
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
def findBestShift(wordList, text): | |
""" | |
Finds a shift key that can decrypt the encoded text. | |
text: string | |
returns: 0 <= int < 26 | |
""" | |
### TODO | |
bestShift=0 | |
ValidWords=0 | |
WordsFound=0 | |
#Set the maximum number of real words found to 0 | |
#Set the best shift to 0 | |
#For each possible shift from 0 to 26: | |
for shift in range (25): | |
#print shift | |
#shift the entire text by this shift | |
applyShift(text, shift) | |
#split the text up into a list of the individual words | |
text.split(' ') | |
#count the number of valid words in this list | |
for i in range(len(text)): | |
if text[i] in wordList: | |
ValidWords += 1 | |
#if the humber of valid words is more than the largest number of real words found, then: | |
if ValidWords > WordsFound: | |
#record the number of valid words | |
WordsFound = ValidWords | |
#set the best shift to current shift | |
bestShift = shift | |
#increment the current possible shift by1. repeat the loop | |
shift +=1 | |
#return the best shift | |
return bestShift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment