Skip to content

Instantly share code, notes, and snippets.

@doraneko94
Last active November 6, 2018 06:54
Show Gist options
  • Save doraneko94/553e0dd6d704659c09e5b4277ae6fdd5 to your computer and use it in GitHub Desktop.
Save doraneko94/553e0dd6d704659c09e5b4277ae6fdd5 to your computer and use it in GitHub Desktop.
The improved version of "caesar.py" using a list of 153,484 words. "dictionary.pkl" is available from http://ushitora.net/archives/456 .
import pickle
ALPHA = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
word = input("Please Enter Words: ")
def plus_i(i, w):
if w in alpha:
n = alpha.index(w) + i
if n > 25:
n -= 26
return alpha[n]
elif w in ALPHA:
n = ALPHA.index(w) + i
if n > 25:
n -= 26
return ALPHA[n]
else:
return w
candidates = ["".join([plus_i(i, w) for w in word]) for i in range(26)]
with open("dictionary.pkl", "rb") as f: # "dictionary.pkl" is available from
obj = pickle.load(f) # http://ushitora.net/archives/456
f.close() # The list of 153,484 words in about 100 books.
c_scores = [sum([1 if c.lower() in obj else 0 for c in candidate.split(" ")]) for candidate in candidates]
print(candidates[c_scores.index(max(c_scores))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment