[Tutor] Anagram creator

btkuhn at email.unc.edu btkuhn at email.unc.edu
Sun Dec 28 01:32:29 CET 2008


Hi everyone,

I'm having trouble with an anagram generating program that I am writing 
in Python. My output is not what it should be and I think the reason 
has something to do with my helper functions creating a reference to a 
dictionary each time it is called rather than a new "clone" dictionary. 
Here is my main function, with explanation afterward:


    def anagramsHelper(phrase,dictionary,anagramsofar):        if 
isSolved(phrase): #returns true if the dictonary for the phrase is empty
            print anagramsofar
                      return True
        for word in dictionary:
            if wordCanBeMade(word, phrase):
                anagramsofar=anagramsofar+" "+word
                phraseclone=removeLetters(word, phrase)
                anagramsHelper(phraseclone,dictionary,anagramsofar)



Initially it takes a dictionary representing a word or phrase (eg 
{'a':2,'d':1, 'e':2}), a dictionary in the form of a list of words, and 
anagramsofar which is initially passed as "".
Next, isSolved returns true if the dictionary is empty, meaning that 
all letters in the phrase have been used up. Otherwise, for each word 
in the dictionary, the program tests whether the word can be made from 
letters in the phrase. If it can, the word is appended to phrasesofar. 
removeLetters then removes the letters that were used to make the word. 
I saved the new phrase as phraseclone as an attempt to troubleshoot; 
originally I just used the original phrase variable. Next, the function 
is recursively called with the letters removed from the phrase. The end 
result is that when the phrase dictionary is empty, "anagramsofar" is 
printed. This should generate all anagrams of a given phrase using a 
given dictionary of words.

So, for instance, I might call (from my main function):

dictionary=importDictionary()  #A list of 10,000 strings, representing 
words in the dictionary

anagramsHelper("george bush",dictionary,"")



wordCanBeMade returns a boolean "T" or "F", and I've posted the code 
for removeLetters below.

My code is not working as expected, and after a good deal of debugging 
it seems that during the recursive calls, when I remove letters from 
the dictionary they "stay removed", even though I'm trying to create a 
new dictionary with each call (so that removed letters are not 
remembered). As a result, anagramsofar gets longer with every call and 
isSolved returns True every time, since all keys are removed after the 
first few calls. Here is the function for removeLetters:


    def removeLetters(word, phrase):
        phraseclone=phrase.copy()
        for letter in word:
            phraseclone[letter]=phraseclone[letter]-1
              return phraseclone



I'm trying to create a copy of phrase with each function call and then 
return the copy to prevent the program remembering the removed 
keys...but it's not working. Does anyone see what I'm doing wrong? I 
can post the entire program if needed; I just wanted to keep things 
somewhat concise.

Thanks.


More information about the Tutor mailing list