I need to generate all possible words of Ki lengths given n characters for example:
given
LNDJOBEAWRL
do bear
I cant come up with the word of len 5 but this is the idea
n = 11
k1 = 2
k2 = 4
k3 = 5
So basically all words of length 2 4 and 5 but without reusing characters. What would be the best way to do it?
My dictionary structure looks likes this:
{
3: [{u'eit': u' "eit":0'}],
5: [{u'doosw': u' "woods": 4601, '}, {u'acenr': u' "caner": 0, '}, {u'acens': u' "canes": 0, '}, {u'acden': u' "caned": 0, '}, {u'aceln': u' "canel": 0,'}],
6: [{u'abeill': u' "alible": 0, '}, {u'cdeeit': u' "deciet":0,'}, {u'demoor': u' "mooder": 0, '}],
7: [{u'deiprss': u' "spiders": 0, '}, {u'deiprsy': u' "spidery": 0, '}, {u'cersttu': u' "scutter": 0, '}],
8: [{u'chiiilst': u' "chilitis": 0, '}, {u'agilnrtw': u' "trawling": 0, '}, {u'abdeemns': u' "beadsmen": 0, '}],
9: [{u'abeiilnns': u' "biennials": 0, '}, {u'bclooortu': u' "oblocutor": 0, '}, {u'aabfiinst': u' "fabianist": 0, '}, {u'acdeiituz': u' "diazeutic": 0, '}, {u'aabfiimns': u' "fabianism": 0, '}, {u'ehnoooppt': u' "optophone": 0, '}],
10: [{u'aiilnoprtt': u' "tripolitan": 0, '}, {u'eeilprrsty': u' "sperrylite": 0, '}, {u'gghhiilttt': u' "lighttight": 0, '}, {u'aeegilrruz': u' "regularize": 0, '}, {u'ellnprtuuy': u' "purulently": 0, '}],
11: [{u'cdgilnoostu': u' "outscolding": 0, '}],
12: [{u'ceeeilnostuy': u' "leucosyenite": 0, '}, {u'aacciloprsst': u' "sarcoplastic": 0, '}],
13: [{u'acdeimmoprrsu': u' "cardiospermum": 0, '}, {u'celnnooostuvy': u' "noncovetously": 0, '}],
14: [{u'adeejmnnoprrtu': u' "preadjournment": 0, '}]
}
And my modified code looks like this:
wlen = self.table[pos]
if pos == 0:
# See if the letters remaining in the bag are a valid word
key = ''.join(sorted(bag.elements()))
for d in wlen:
if key in d.keys():
yield solution + [key]
else:
pos -= 1
for dic in wlen:
print(len(dic))
for key in dic.keys():
The code below uses a recursive generator to build solutions. To store the target letters we use a
collections.Counter, this acts like a set that permits repeated items.To simplify searching we create a dictionary for each word length that we want, storing each of those dictionaries in a dictionary called
all_words, with the word length as the key. Each sub-dictionary stores lists of words containing the same letters, with the sorted letters as the key, eg'aet': ['ate', 'eat', 'tea'].I use the standard Unix '/usr/share/dict/words' word file. If you use a file with a different format you may need to modify the code that puts words into
all_words.The
solvefunction starts its search with the smallest word length, and works up to the largest. That's probably the most efficient order if the set containing the longest words is the biggest, since the final search is performed by doing a simple dict lookup, which is very fast. The previous searches have to test each word in the sub-dictionary of that length, looking for keys that are still in the target bag.output
FWIW, here are the results for
output
This code was written for Python 3. You can use it on Python 2.7 but you will need to change
to