Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
while lexicon[length]:
word = lexicon[length].pop()
if rotations(word).issubset(lexicon[length]):
print word, rotations(word)
$ python /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 /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