I know variations of this have been asked before, but I was unable to understand any of the previous implementations because most of them involved using sets and the issubset method.

Here is what I am trying to do: I have a set of words in a dictionary and a list of possible letters. I want to find if the members of the set can be formed through re-arranging the letters in the list. Here is my current implementation:

def solve(dictionary, letters):
for word in dictionary: #for each word in the dictionary
if len(word) > len(letters): # first optimization, doesn't check words that are larger than letter set
continue
else:
scrambledword = "".join([b for b in sorted(list(word))]) #sorts the letters in each word
if set(scrambledword).issubset(letters):
print word
def main():
dictionary = set([x.strip() for x in open("C:\\Python27\\dictionary.txt")])
letters = sorted(['v','r','o','o','m','a','b','c','d'])
solve(dictionary, letters)
main()

The obvious problem with this implementation is that some words will be found that use more than one letter in "letters." For example, the word 'cardboard' appears as a valid word, despite there being only one copy of 'a' and 'r' in the letters list. How do I use the "issubset" method on lists?

3 Answers
3

To know if you can make a word out of a set of letters [oops, I did it myself -- I meant 'collection'!], you want every letter to occur at least the right number of times, so I think we're going to have to work the counts in there somehow. By definition, Python sets don't care about the number of elements in a source list. Maybe something like

It works well, but is prohibitively slow on very large lists. Let me make sure that I get how it works: so the loop will run through all letters in the word, insuring that every the frequency of letters in the "letter list" is greater than or equal to the frequency of letters in the actual word? Pretty smart implementation. How would I go about printing only the maximum lettered word? I want to use the "max()" function, but I don't know how it would work.
–
ParseltongueJun 13 '11 at 4:55

I may have to take back the comment about it being "prohibitively slow", as I've now tested out previous implementations that I saw on other threads, and they are all exponentially slower. Kudos; but I'm still looking for any optimizations to this.
–
ParseltongueJun 13 '11 at 5:02