I solved the problem using this (horribly inefficient method):
def createList(word, wordList):
#Made a set, because for some reason permutations was returning duplicates.
#Returns all permutations if they're in the wordList
return set([''.join(item) for item in itertools.permutations(word) if ''.join(item) in wordList])
def main():
a = open('C:\\Python32\\megalist.txt', 'r+')
wordList = [line.strip() for line in a]
maximum = 0
length = 0
maxwords = ""
for words in wordList:
permList = createList(words, wordList)
length = len(permList)
if length > maximum:
maximum = length
maxwords = permList
print (maximum, maxwords)
It took something like 10 minutes to find the five-letter word that has the most dictionary-valid anagrams. I want to run this on words without a letter constraint, but it would take a ludicrous amount of time. Is there anyway to optimize this?
This following seems to work OK on a smallish dictionary. By sorting the letters in the word, it becomes easy to test if two words are an anagram. From that starting point, it’s just a matter of accumulating the words in some way. It wouldn’t be hard to modify this to report all matches, rather than just the first one
If you do need to add constraints on the number of letters, the use of iterators is a convenient way to filter out some words.
Edit:
In response to the comment, the
hash(sortedWord), is a space saving optimization, possibly premature in this case, to reduce sortedWord back to an integer, because we don’t really care about the key, so long as we can always uniquely recover all the relevant anagrams. It would have been equally valid to just usesortedWordas the key.The
keykeyword argument tomaxlets you find the maximum element in a collection based on a predicate. So the statementmaxKey = max( d.keys(), key = lambda k : len(d[k]) )is a succinct python expression for answering the query, given the keys in the dictionary, which key has the associated value with maximum length?. That call tomaxin that context could have been written (much more verbosely) asvalueWithMaximumLength(d)where that function was defined as: