Python Word Unscrambler:

OK, this is the Python code I'm using to unscramble words but it unscrambles one word at a time, can someone please help me out and tell me how can I improve my code to make it decrypt several words at a time?
P.S: Another thing is that it prints the solution 4 or 5 times don't know why.

Sorry, I'm very new to Python.
Please help if you can.
Thanks a ton!
------------------------------------------------------------------------------------------------------------

CODE

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(wordlist):
    dict = {}
    infile = open(wordlist, "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 words you need to unscramble: ")
    anaLst = anagrams(anagram)
    diction = dictionary("wordlist.txt")
    for ana in anaLst:
        if diction.has_key(ana):
            print "The solution to the jumble is", ana

main()

------------------------------------------------------------------------------------------------------------

 

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to