-3
我們將爲26個可能的密鑰中的每一個生成解密字母。Python凱撒休息時間
我已經完成了該部分,但無法打印出字典中的單詞。 例如:我只想'ebv'打印嘿
如何將它匹配到詞典中的單詞而不是打印出所有26個可能的鍵。
這裏的字典:http://www.ics.uci.edu/~kay/wordlist.txt
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
def rotated_alphabet(key:int) -> str:
'''produces a rotated alphabet based on key and adds those letters to the end of alphabet'''
if key > 26:
key = key % 26
new_alphabet = ''
w = Alphabet[0:key]
x = Alphabet.replace(Alphabet[0:key], new_alphabet)
return (x + w)
def Caesar_break(cipher:str) ->str:
infile = open('wordlist.txt', 'r')
wordlist = []
possible = []
decode = []
words = []
for str in infile:
t = str
for i in range(0, 26):
p = rotated_alphabet(i).split()
possible+=p
for y in possible:
decrypt = str.maketrans(y, Alphabet)
decode.append(cipher.translate(decrypt))
for str in decode:
s = str
words.append(s)
print(words)
Caesar_break('eby')
它打印出:
['ebv', 'dau', 'czt', 'bys', 'axr', 'zwq', 'yvp', 'xuo', 'wtn', 'vsm', 'url', 'tqk', 'spj', 'roi', 'qnh', 'pmg', 'olf', 'nke', 'mjd', 'lic', 'khb', 'jga', 'ifz', 'hey', 'gdx', 'fcw']
**你有什麼** **?你卡在哪裏? –
是'def Caesar_break(cipher:str) - > str:'有效的Python? –
@hayden:是的,雖然在這裏不是很有用。參見[PEP 3107](http://www.python.org/dev/peps/pep-3107/#fundamentals-of-function- annotations)和[ref](http://docs.python.org/3.3 /reference/compound_stmts.html#function-definitions)。 – DSM