[Tutor] Python Word Unscrambler
Null Mr.Freeman
null.scripter at gmail.com
Thu Jun 22 15:45:16 CEST 2006
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()
------------------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060622/5a1f4d92/attachment.html
More information about the Tutor
mailing list