Skip to content

Instantly share code, notes, and snippets.

@andreasvc
Created January 10, 2013 22:27
Show Gist options
  • Save andreasvc/4506363 to your computer and use it in GitHub Desktop.
Save andreasvc/4506363 to your computer and use it in GitHub Desktop.
Given a dictionary, find words for which all rotations occur in the dictionary.
import sys, collections
def rotations(a):
return {a[x:] + a [:x] for x in range(1, len(a))}
lexicon = collections.defaultdict(set)
for a in open(sys.argv[1]):
lexicon[len(a) - 1].add(a.strip())
for length in sorted(lexicon):
if length == 1:
continue
while lexicon[length]:
word = lexicon[length].pop()
if rotations(word).issubset(lexicon[length]):
lexicon[length].difference_update(rotations(word))
lexicon[length].difference_update({word})
print word, rotations(word)
$ python draaideur.py /usr/share/dict/american-english
ha set(['ah'])
ti set(['it'])
ma set(['am'])
em set(['me'])
eh set(['he'])
um set(['mu'])
no set(['on'])
oh set(['ho'])
spa set(['pas', 'asp'])
eat set(['tea', 'ate'])
$ python draaideur.py /usr/share/dict/dutch
BG set(['GB'])
oh set(['ho'])
la set(['al'])
ra set(['ar'])
dc set(['cd'])
af set(['fa'])
ge set(['eg'])
SP set(['PS'])
ei set(['ie'])
IX set(['XI'])
es set(['se'])
er set(['re'])
ms set(['sm'])
MA set(['AM'])
op set(['po'])
is set(['si'])
ha set(['ah'])
as set(['sa'])
ai set(['ia'])
ik set(['ki'])
nee set(['een', 'ene'])
ere set(['eer', 'ree'])
ast set(['sta', 'tas'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment