Word Permutations

We were discussing something yesterday and it came up for us to arrive at all the possible words with at least 3 letters using the letters in the word ‘football’. We started out with a  paper and pencil and came up with about 2 dozens when it struck me that I could do this with python.

The pseudo code in my mind was this:

1) generate all permutations/combinations of the letters in the word

2) check against a dictionary to see if what you generate is valid

3) uniquify

I have checked in my snippet in github. Feel free to point out the errors.

import itertools
import enchant

d1 = enchant.Dict("en_UK")
d2 = enchant.Dict("en_US")
l = 'football'
wl = {}
for i in range(3, len(l)+1) :
	for each in itertools.permutations(l, i) :
		w = ''.join(each)
		if d1.check(w) or d2.check(w) :
			wl[w] = None

print len(wl.keys())
print wl.keys()			

Cloud

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.