944,131 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 46056
  • Python RSS
Jun 22nd, 2006
-1

word unscrambler

Expand Post »
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
Python Syntax (Toggle Plain Text)
  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()
Similar Threads
zx9
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zx9 is offline Offline
1 posts
since Jun 2006
Jun 24th, 2006
0

Re: word unscrambler

What do you want to do if there is no solution?
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 25th, 2006
0

Re: word unscrambler

I took your code and changed it mildly to give you an idea how to proceed ...
Python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 5th, 2006
0

Re: word unscrambler

I liked your recursive anagram creator, but it is expensive because it generates n! strings for a size n scramble. I tested it with

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Extract header info from image file
Next Thread in Python Forum Timeline: Client Server Interaction





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC