Skip to content

Instantly share code, notes, and snippets.

@rgov
Created July 9, 2012 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgov/3073906 to your computer and use it in GitHub Desktop.
Save rgov/3073906 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import itertools, re
def explode(str):
return re.split(r'(\W+)', str)[:-1]
def reverseinplace(str):
r = [ w[::-1] if re.match(r'\w+$', w) else w for w in explode(str) ]
return ''.join(r)
def pads(length):
for c in itertools.product([-1, 0, 1], repeat=length):
yield c
def applypad(str, pad):
return ''.join( chr((ord(c) - ord('A') + p) % 26 + ord('A')) \
for c, p in zip(str, pad) )
def findfuzzyword(word, wordlist):
out = set()
for p in pads(len(word)):
fuzzed = applypad(word, p)
if fuzzed in wordlist:
out.add(fuzzed)
return out
cipher = 'Eiv eanegduce rg div trec diduj en gniityna. ydit tnonac fc ddik ' \
'mu. b tnemom tciv piaj epweiv bommi gitna.'
cipher = cipher.upper()
words = set( w.upper().rstrip() for w in file('/usr/share/dict/words') )
reversed = reverseinplace(cipher)
for w in explode(reversed):
if not re.match(r'\w+$', w):
continue
print w, '->', findfuzzyword(w, words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment