import string def buildCoder(shift): """ Returns a dict that can apply a Caesar cipher to a letter. The cipher is defined by the shift value. Ignores non-letter characters like punctuation, numbers, and spaces. shift: 0 <= int < 26 returns: dict """ charDict = {} upperCaseShift = ord('A')-ord('a') alphabetLen = ord('z')-ord('a') + 1 for charNum in range (alphabetLen): position_left = charNum + ord('a') position_rigt = (charNum + shift) % alphabetLen + ord('a') charDict[chr(position_left)]=chr(position_rigt) charDict[chr(position_left+upperCaseShift)]=chr(position_rigt+upperCaseShift) return charDict def applyCoder(text, coder): """ Applies the coder to the text. Returns the encoded text. text: string coder: dict with mappings of characters to shifted characters returns: text after mapping coder chars to original text """ encryptedText = str() # print string.ascii_lowercase + '___' + string.ascii_uppercase for char in text : # print char if (char in string.ascii_lowercase) or (char in string.ascii_uppercase) : # print "IN IF" # print coder[char] encryptedText += coder[char] # print encryptedText else : encryptedText += char return encryptedText def applyShift(text, shift): """ Given a text, returns a new text Caesar shifted by the given shift offset. Lower case letters should remain lower case, upper case letters should remain upper case, and all other punctuation should stay as it is. text: string to apply the shift to shift: amount to shift the text (0 <= int < 26) returns: text after being shifted by specified amount. """ return applyCoder (text, buildCoder (shift)) #---------------------------------------------------------------------------- def clearNull(listOfWords): return [x for x in listOfWords if x != ''] def splitToWords(listOfWords, specialCharachters): if specialCharachters == '': return listOfWords else: newList = [] for word in listOfWords: newList += word.split(specialCharachters[0]) newList = clearNull (newList) return splitToWords (newList, specialCharachters[1:]) def textToList (text): specialCharachters = string.digits + string.punctuation + string.whitespace return splitToWords ([text], specialCharachters) def countWords (wordList, text): listOfWords = textToList (text) counter = 0 for word in listOfWords: if word in wordList : counter += 1 return counter def findBestShift(wordList, text): """ Finds a shift key that can decrypt the encoded text. text: string returns: 0 <= int < 26 """ listOfNumbers = [] for i in range (26): listOfNumbers.append( countWords (wordList, applyShift(text, i))) return listOfNumbers.index (max (listOfNumbers)) print findBestShift(['Hello', 'world'], applyShift('Hello, world!', 6))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question