Hi, yesterday I threw together a little jumble solver that takes any anagram as an input and returns any real words that can be found using all of the letters of that anagram. Here is the code, it works fine when it comes to determining the answer, but I have a couple of questions.

# jumble.py
import string

def anagrams(s):
    if s == "":
        return [s]
    else:
        ans = []
        for an in anagrams(s[1:]):
            for pos in range(len(an)+1):
                ans.append(an[:pos]+s[0]+an[pos:])
        return ans

def dictionary(filename):
    dict = {}
    infile = open(filename, "r")
    for line in infile:
        word = line.split("\n")[0]
        dict[word] = 1
    infile.close()
    return dict

def main():
    anagram = raw_input("Please enter a word you need to unscramble: ")
    anaLst = anagrams(anagram)
    diction = dictionary("dictionary.txt")
    for ana in anaLst:
        if diction.has_key(ana):
            print "The solution to the jumble is", ana

main()

My questions are:
1) Can anyone please explain to me why for some anagrams like ouhse (house) I only get the answer printed once while with other anagrams such as dunretnda (redundant) I get the answer printed multiple times (in this case, 4)?

2) Is there another way to write this code so I can solve much bigger anagrams without receiving a memory error or am I strictly locked down by the amount of ram I have on my computer? (*shakes fist at factorials* :p )

Thank you so much for your time and help.

Not being able to add a whole lot to your code, I'm willing to bet that the reason why dunretnda (redundant) shows up more than one time is because there are duplicate characters in the string. For instance, in the case of dunretnda, position 2 == 'n', and position 6 == 'n', as well. Even though each position has the same literal character in it, it's still at a different position in the string.

Also, one way you could get around not running out of memory is to formulate some way to write the results to a temporary text file, just to purge it from RAM. It's been a while since I've programmed Python, but you could then manipulate the results stored in that file using streams, so you'd make more efficient use of the RAM available in your system. You could use a stream to write lines to a file, and then use a stream to read lines from the file, one by one. I think that Python allows you to set the position in a file, too, so you could still reference particular lines in the file, if you needed.

Excellent! You were exactly right. The amount of multiple answers is determined by how many of the same characters are in the anagram and how many sets of multiple characters there are. The streaming also works extremely well. Deciphering yonuxrcdelciibeo is now a piece of cake :) Thanks a lot.

hey there mate, im in the process of writing my own anagram solver and stumbled across your post, your code dosnt seem to work for me, i have the dic text file and everything seems perfect just when i enter a anagram i know is in the dict file the app just closes.

:~/Desktop$ python anagramsolver.py
Please enter a word you need to unscramble: strops
t:~/Desktop$

srry to hijack your thread any help would be awesome.

The standard solution for anagrams is to sort on the letters. So 'ogd' sorted is 'dgo', and a lookup of the 'dgo' key in the dictionary yields the list ["dog", "god"]. Your code would only return whichever one of these words it found first. Also, you should not use "dict" as a variable name as it is a reserved word. It is a good idea to use 2 words for a variable name to avoid possible conflicts. "words_dict" would be a better choice for example.

commented: very good help +12

There are many of jumble websites are available online i think you can check there to clear your confusion. Some of them i've seen personally that are using multiple answers to the jumble words function and are working fine.

amazing job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.