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 """ ### TODO import string keydic = {} for i in range(26): if i + shift > 25: shifter = i + shift - 25 else: shifter = i + shift keydic.update({string.ascii_lowercase[i]: string.ascii_lowercase[shifter]}) keydic.update({string.ascii_uppercase[i]: string.ascii_uppercase[shifter]}) sorted(keydic) return keydic 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 """ ### TODO import string st = "" i=0 while i < len(text): if text[i].lower() in string.ascii_lowercase: st += coder[text[i]] else: st += text[i] i+=1 return st 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. """ ### TODO. ### HINT: This is a wrapper function. return applyCoder(text, buildCoder(shift)) def findBestShift(wordList, text): """ Finds a shift key that can decrypt the encoded text. text: string returns: 0 <= int < 26 """ ### TODO #texto = text import re winj=0 winc=0 for j in range(26): text = applyShift(texto,j) text = re.sub('[^0-9a-zA-Z]+', ' ', text) s = text.split(' ') word = "" i=0 c = 0 while i < len(s): if isWord(wordList, s[i]) == True: c+=1 i+=1 if c > winc: winj = j return winj def worldchunk(text): word="" i = 1 c = 0 while i < len(text): print [text[i] print i if text[i] == " ": print word word = "" else: word += text[i] i+=1 return text print applyShift("Ftq fQmotqd'e zMyq ue Fmnuftm?",22)
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