473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with word unscrambler code for newbie?

Hi, I'm new to Python and need some help with this program. I'm trying to take 10 jumbled words at a time and have the program unscramble them and then print them out like: word1,word2,word3, etc.
So far the code will unscramble more than one word at once but it will print the answer several times if there is more than 1 of the same letter in the word and it will print them in a column. Example is inwdwo and garnama (window anagram). It will print them like this:
window
window
anagram
anagram
anagram.
I read that a set can help this but I'm not sure where to put that or how to get the %s,%s,%s type of print out. Any help?
Expand|Select|Wrap|Line Numbers
  1.  
  2. import string
  3. def anagrams(s):
  4.     if s == "":
  5.         return [s]
  6.     else:
  7.         ans = []
  8.         for an in anagrams(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.         dict[word] = 1
  19.     infile.close()
  20.     return dict
  21.  
  22. def main():
  23.     anagram = raw_input("Please enter words: ")
  24.     wordLst = anagram.split(None)
  25.     diction = dictionary("wordlist.txt")
  26.     for word in wordLst:
  27.         anaLst = anagrams(word)
  28.             for ana in anaLst:
  29.             if diction.has_key(ana):
  30.                 diction[ana] = word
  31.                 print " ", ana  
  32.  
  33. main()
  34.  
Jan 30 '07 #1
12 14491
bvdet
2,851 Expert Mod 2GB
You were very close. This seems to work:
Expand|Select|Wrap|Line Numbers
  1. # inwdwo garnama (window anagram)
  2.  
  3. import string
  4. def anagrams(s):
  5.     if s == "":
  6.         return [s]
  7.     else:
  8.         ans = []
  9.         for an in anagrams(s[1:]):
  10.             for pos in range(len(an)+1):
  11.                 ans.append(an[:pos]+s[0]+an[pos:])
  12.         return ans
  13.  
  14. def dictionary(wordlist):
  15.     dict = {}
  16.     infile = open(wordlist, "r")
  17.     for line in infile:
  18.         word = line.split("\n")[0]
  19.         dict[word] = 1
  20.     infile.close()
  21.     return dict
  22.  
  23. def main(fn, s):
  24.     # anagram = raw_input("Please enter words: ")
  25.     anagram = s
  26.     wordLst = anagram.split(None)
  27.     diction = dictionary(fn)
  28.     outStr = ""
  29.     for word in wordLst:
  30.         anaLst = anagrams(word)
  31.         for ana in anaLst:
  32.             if diction.has_key(ana):
  33.                 diction[ana] = word
  34.                 outStr += '%s ' % (ana)
  35.                 break
  36.     print outStr
  37.  
  38. import os
  39.  
  40. fn = (os.path.join('H:\\', 'TEMP', 'temsys', 'anagrams.txt'))
  41.  
  42. s = 'inwdwo garnama'
  43. main(fn, s)
>>> window anagram
>>>
There were multiple occurrences of 'window' and 'anagram' returned by anagrams(), and 'break' took care of that. Ideally anagrams() should only return one of each possible combination.
Jan 30 '07 #2
bvdet
2,851 Expert Mod 2GB
If you are in Python 2.4 and above, this will eliminate duplicates from the list:
Expand|Select|Wrap|Line Numbers
  1. def anagrams(s):
  2.     if s == "":
  3.         return [s]
  4.     else:
  5.         ans = []
  6.         for an in anagrams(s[1:]):
  7.             for pos in range(len(an)+1):
  8.                 ans.append(an[:pos]+s[0]+an[pos:])
  9.         return set(ans)
  10.  
Or:
Expand|Select|Wrap|Line Numbers
  1. def anagrams(s):
  2.     if s == "":
  3.         return [s]
  4.     else:
  5.         ans = set()
  6.         for an in anagrams(s[1:]):
  7.             for pos in range(len(an)+1):
  8.                 ans.add(an[:pos]+s[0]+an[pos:])
  9.         return ans
Python 2.3:
Expand|Select|Wrap|Line Numbers
  1. def anagrams(s):
  2.     if s == "":
  3.         return [s]
  4.     else:
  5.         ans = []
  6.         for an in anagrams(s[1:]):
  7.             for pos in range(len(an)+1):
  8.                 ans.append(an[:pos]+s[0]+an[pos:])
  9.                 [i]u={}
  10.         for i in ans:
  11.             u=1
  12.         return u.keys()
Jan 30 '07 #3
Thank you, Bvdet for the replies! I'm using 2.5. I tried your suggestions and this part worked for the repeating words part:
Expand|Select|Wrap|Line Numbers
  1. else:
  2.         ans = set()
  3.         for an in anagrams(s[1:]):
  4.             for pos in range(len(an)+1):
  5.                 ans.add(an[:pos]+s[0]+an[pos:])
  6.         return ans
  7.  
Jan 30 '07 #4
bvdet
2,851 Expert Mod 2GB
Thank you, Bvdet for the replies! I'm using 2.5. I tried your suggestions and this part worked for the repeating words part:
Expand|Select|Wrap|Line Numbers
  1. else:
  2.         ans = set()
  3.         for an in anagrams(s[1:]):
  4.             for pos in range(len(an)+1):
  5.                 ans.add(an[:pos]+s[0]+an[pos:])
  6.         return ans
  7.  
You are welcome!
Jan 30 '07 #5
What would you suggest for these next issues? The code now works to unscramble 10 words at a time, taken from a specific wordlist but there are only 30 sec.s to complete it. The scrambled words appear in a column but need solved in single line, seperated by comas.
Scrambled:
irsitp
aiigabl
ttgrae
pmohnat
relkil
ciusrt
aegnor
nceahc
gishof
jme1sa
Solution:spirit,abigail,target,phantom,killer,curt is,orange,chance,gofish,james1
I can't copy and paste the scrambled words in a column because it will only take the 1st word in that format. Is there a way to have the code accept the words like that? Maybe I should have an empty *.txt file, copy and paste to it, then have the code point to it to unscramble? Or, would something like TkInter be helpful?
Jan 30 '07 #6
Well, I did the "Hello, World" in TkInter and after looking over the documentation for it, I think I'd be getting in way over my head at this point! I'll stick to the basics for now :)
Jan 30 '07 #7
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. fn1 = 'your_file.txt'
  2. sLst = []
  3. f = open(fn1, 'r')
  4. for line in f:
  5.     sLst.append(line.strip())
  6. f.close()
  7.  
  8. s = " ".join(sLst)
  9. print s
  10. main(fn, s)
Yields:
>>> irsitp aiigabl ttgrae pmohnat relkil ciusrt aegnor nceahc gishof jme1sa
spirit abigail target phantom killer curtis orange chance gofish james1

This may not be the best way, but it works. :)
Jan 30 '07 #8
bvdet, for some reason when I tried that it doesn't work for me so I've come up with this:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     anagram = raw_input("Please enter words: ")
  3.     wordLst = anagram.split(None)
  4.     diction = dictionary("wordlist.txt")
  5.     for word in wordLst:
  6.         anaLst = anagrams(word)
  7.         for ana in anaLst:
  8.             if diction.has_key(ana):
  9.                 diction[ana] = word
  10.                 Solution = ana+','
  11.                 solutionWrite = open('solution.txt','w')
  12.                 solutionWrite.write(Solution)
  13. main()
  14.  
  15.  
This is printing to solution.txt with a comma behind the word BUT it's only printing one unscrambled word. If I enter:irispt
I can open the text file and see:
spirit,
Great. Now I try: irispt ubasc1 bbu1ab
and it writes:
bubba1,
It's only writing the last unscrambled word.
Feb 1 '07 #9
Here's the whole code so far:
Expand|Select|Wrap|Line Numbers
  1. import string
  2. def anagrams(s):
  3.     if s == "":
  4.         return [s]
  5.     else:
  6.         ans = set()
  7.         for an in anagrams(s[1:]):
  8.             for pos in range(len(an)+1):
  9.                 ans.add(an[:pos]+s[0]+an[pos:])
  10.         return ans
  11.  
  12. def dictionary(wordlist):
  13.     dict = {}
  14.     infile = open(wordlist, "r")
  15.     for line in infile:
  16.         word = line.split("\n")[0]
  17.         dict[word] = 1
  18.     infile.close()
  19.     return dict
  20.  
  21. def main():
  22.     anagram = raw_input("Please enter words: ")
  23.     wordLst = anagram.split(None)
  24.     diction = dictionary("wordlist.txt")
  25.     for word in wordLst:
  26.         anaLst = anagrams(word)
  27.         for ana in anaLst:
  28.             if diction.has_key(ana):
  29.                 diction[ana] = word
  30.                 Solution = ana+','
  31.                 solutionWrite = open('solution.txt','w')
  32.                 solutionWrite.write(Solution)
  33. main()
  34.  
Feb 1 '07 #10
bvdet
2,851 Expert Mod 2GB
I changed a few things but have not tested it. The solution string is initialized before the loop. The solution text is concatenated to it in the loop. The complete solution string is written to file after the iterations are complete. The slice '[:-2]' trims off the trailing comma and space. It is good practice to close each file that you open. I personally like to use single letters such as 'f' for file in file operations. It's easier to read when there are fewer letters and a descriptive name is not necessary.

Check out Guido's Python style guide for some useful tips. http://www.python.org/doc/essays/styleguide/
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     anagram = raw_input("Please enter words: ")
  3.     wordLst = anagram.split(None)
  4.     diction = dictionary("wordlist.txt")
  5.     solution = ""
  6.     for word in wordLst:
  7.         anaLst = anagrams(word)
  8.         for ana in anaLst:
  9.             if diction.has_key(ana):
  10.                 diction[ana] = word
  11.                 solution += '%s, ' % (ana)
  12.     f = open('solution.txt','w')
  13.     f.write(solution[:-2])
  14.     f.close()
  15. main()
  16.  
Feb 1 '07 #11
I changed a few things but have not tested it. The solution string is initialized before the loop. The solution text is concatenated to it in the loop. The complete solution string is written to file after the iterations are complete. The slice '[:-2]' trims off the trailing comma and space. It is good practice to close each file that you open. I personally like to use single letters such as 'f' for file in file operations. It's easier to read when there are fewer letters and a descriptive name is not necessary.

Check out Guido's Python style guide for some useful tips. http://www.python.org/doc/essays/styleguide/
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     anagram = raw_input("Please enter words: ")
  3.     wordLst = anagram.split(None)
  4.     diction = dictionary("wordlist.txt")
  5.     solution = ""
  6.     for word in wordLst:
  7.         anaLst = anagrams(word)
  8.         for ana in anaLst:
  9.             if diction.has_key(ana):
  10.                 diction[ana] = word
  11.                 solution += '%s, ' % (ana)
  12.     f = open('solution.txt','w')
  13.     f.write(solution[:-2])
  14.     f.close()
  15. main()
  16.  
bvdet, you are my hero! This works! Thanks again for your time and knowledge!
Now I'll finish it off by adding #comments before moving on to another project. Also, thanks for the link and for helping me 'clean-up' the code by keeping it simpler.
Feb 1 '07 #12
bvdet
2,851 Expert Mod 2GB
bvdet, you are my hero! This works! Thanks again for your time and knowledge!
Now I'll finish it off by adding #comments before moving on to another project. Also, thanks for the link and for helping me 'clean-up' the code by keeping it simpler.
My pleasure. This was an interesting problem. Your positive comments make it worth the effort!
Feb 1 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: William Ashworth | last post by:
I have this glossary that was setup and I want to modify a bit. Here's how... ############################## printf($getWordArray); echo "</b></dt><dd>"; printf($getWordArray); echo...
5
by: Alistair | last post by:
Hello folks... this is my first post in here. I'm new to ASP having done all my previous work in Flash and bog standard HTML. Only been learning for a couple of weeks. anyway...I have been...
9
by: tiger79 | last post by:
Hi, I've been translating some API's from VB to C# for a couple of days now. Only I'm a real newbie to both languages, but I managed to do most of it except the next statements, so if anyone knows...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
4
by: Peppie | last post by:
Hi all, I created a button in an Access form that opens MS Word. Now I want this button also to open a specific file. How do I code this? Would it also be possible to let the user select the...
3
by: pagates | last post by:
Hello All, This is a newbie follow-up to my previous newbie question.... Is there a way to view a Word document (or a PDF, or a text file, or any other file) as content for a Master Page? ...
6
Gaiason
by: Gaiason | last post by:
Hi XML/XSLT masters and gurus, I am a newbie in XML/XSLT and have been reading up XML/XSLT in order to convert a XML generated from a OCR engine to another format of XML. I am truly stuck at this...
0
by: Tim Golden | last post by:
Lave wrote: You have broadly two approaches here, both involving automating Word (ie using the COM object model it exposes, referred to in another post in this thread). 1) Use the COM model...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.