word unscrambler

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2006
Posts: 1
Reputation: zx9 is an unknown quantity at this point 
Solved Threads: 0
zx9 zx9 is offline Offline
Newbie Poster

word unscrambler

 
0
  #1
Jun 22nd, 2006
hi,
This is my problem i have programmed a little word unscrambler i have a file wordlist.txt in my python folder and this program works just fine and unscrambles one word at a time but i dont how how to edit it to make it unscramble 10 words at a time can someone please help??
thanks
ps i want it to print the solution in this format:
word1, word2, word3,.......all the way to word10
here is my code
ps it prints the same solution multiple times
  1. import string
  2. def anagrams(s):
  3. if s == "":
  4. return [s]
  5. else:
  6. ans = []
  7. for an in anagrams(s[1:]):
  8. for pos in range(len(an)+1):
  9. ans.append(an[:pos]+s[0]+an[pos:])
  10. return ans
  11. def dictionary(wordlist):
  12. dict = {}
  13. infile = open(wordlist, "r")
  14. for line in infile:
  15. word = line.split("\n")[0]
  16. dict[word] = 1
  17. infile.close()
  18. return dict
  19. def main():
  20. diction = dictionary("wordlist.txt")
  21. for i in range(10):
  22. anagram = raw_input("Please enter a words you need to unscramble: ")
  23. anaLst = anagrams(anagram)
  24. for ana in anaLst:
  25. if diction.has_key(ana):
  26. print "The solution to the jumble is", ana
  27. main()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: word unscrambler

 
0
  #2
Jun 24th, 2006
What do you want to do if there is no solution?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: word unscrambler

 
0
  #3
Jun 25th, 2006
I took your code and changed it mildly to give you an idea how to proceed ...
  1. # unscramble a line of words
  2.  
  3. def permutation(s):
  4. if s == "":
  5. return [s]
  6. else:
  7. ans = []
  8. for an in permutation(s[1:]):
  9. for pos in range(len(an)+1):
  10. ans.append(an[:pos]+s[0]+an[pos:])
  11. return ans
  12.  
  13. def dictionary(wordlist):
  14. dict = {}
  15. infile = open(wordlist, "r")
  16. for line in infile:
  17. word = line.split("\n")[0]
  18. # all words in lower case!!!
  19. word = word.lower()
  20. dict[word] = 1
  21. infile.close()
  22. return dict
  23.  
  24. def main():
  25. diction = dictionary("wordlist.txt")
  26.  
  27. # enter all the words that fit on a line or limit the number
  28. anagram = raw_input("Please enter space separated words you need to unscramble: ")
  29. wordLst = anagram.split(None)
  30.  
  31. for word in wordLst:
  32. anaLst = permutation(word)
  33. for ana in anaLst:
  34. if diction.has_key(ana):
  35. diction[ana] = word
  36. #print "The solution to the jumble is" , ana
  37. solutionLst = []
  38. for k, v in diction.iteritems():
  39. if v != 1:
  40. solutionLst.append(k)
  41. print "%s unscrambled = %s" % (v, k)
  42. print solutionLst
  43.  
  44. main()
You could limit the scrambled words entered with a loop.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: word unscrambler

 
0
  #4
Aug 5th, 2006
I liked your recursive anagram creator, but it is expensive because it generates n! strings for a size n scramble. I tested it with

  1. print anagrams("Hi There")
and my reasonably fast machine cried and locked up. :lol:

Another possibility would be to test whether your scramble is an anagram for each word in the list. You could do this by seeing whether each letter in the scramble is somewhere in the word, removing that letter from the word if found. Here's the idea:

  1. def isanagram(source, target):
  2. """returns True if source is an anagram of target, False
  3. otherwise."""
  4.  
  5. # speed-up if your dictionary is large:
  6. # if len(source) != len(target): return False
  7.  
  8. for i in source:
  9. pos = target.find(i)
  10. if pos == -1:
  11. return False
  12. else:
  13. # remove found letter from the target (non-destructively)
  14. target = target[:pos]+target[pos+1:]
  15. return True

In the worst case, this runs O(n^2 * length of dictionary), but with much better performance on average. I found a noticeable speed-up just on this: print isanagram("Hi There", "There Hi")

hope it helps,
Jeff Cagle
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC