Re: [Tutor] Algorithm
Alan, Thanks for the reply. This is not homework, it is a hobby. I am 44 years old. I was using Visual Basic, but recently decided to switch to Linux and have no intentions of going back to windows. Python seems like a good computer language. I read somewhere the the best way to learn it was to pick a project and start programming. Since one of my other hobbies is word games and Linux is severely lacking in this area I decided to write a game similar to Text Twist in windows for my first project. The game I am writing has a graphical front end using GTK and the code so far is long and in more than one file (to keep the gui code seperate). I will gladly email you the whole thing to you if you like. In my research, I looked at examples of permutations, lists, sets, and most recently dictionaries which I have the feeling is the solution. Permutation looked good, but I am an amateur programer and couldn't quite grasp the concepts from the examples I found. Dictionaries hold promise as it can be used to get a count of the letters in words. from operator import itemgetter def countletters(word): lettercount = {} for letter in word: lettercount[letter] =lettercount.get(letter,0) + 1 print sorted(lettercount.iteritems(), key=itemgetter(1)) countletters("batty") [('a', 1), ('y', 1), ('b', 1), ('t', 2)] countletters("bat") [('a', 1), ('b', 1), ('t', 1)] countletters("bats") [('a', 1), ('b', 1), ('s', 1), ('t', 1)] bat is in batty. bats is not. I have a list of words in wordlist.txt. I can write a loop the do the letter counts for each word, but I can't figure out how to compare them. thanx, kreglet Alan Gauld wrote: > > > "kreglet" wrote > >> The problem that I am having is writing an algorithm for finding all the >> possible words from a given word. For example: python > > This sounds a lot like a homework. > We don't give direct help on homeworks but will try to point you > in the right direction. It helps if you tell/show us what you've tried > and where you are stuck. > > How do you think it should work? > Can you think of a systematic approach using pen and paper? > Can you program that? > Does it work? What is wrong? > > Hint: start with a small word that you can check for correctness. > Also test for things like double letters, > eg see. - is ese the same as ese? (the e's are swapped, honest!...) > > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://www.nabble.com/Algorithm-tp25107922p25109886.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
On Sun, Aug 23, 2009 at 10:01 PM, kreglet wrote: > > Alan, > > Thanks for the reply. This is not homework, it is a hobby. I am 44 years > old. I was using Visual Basic, but recently decided to switch to Linux and > have no intentions of going back to windows. Python seems like a good > computer language. I read somewhere the the best way to learn it was to > pick > a project and start programming. Since one of my other hobbies is word > games > and Linux is severely lacking in this area I decided to write a game > similar > to Text Twist in windows for my first project. > > The game I am writing has a graphical front end using GTK and the code so > far is long and in more than one file (to keep the gui code seperate). I > will gladly email you the whole thing to you if you like. > > In my research, I looked at examples of permutations, lists, sets, and > most recently dictionaries which I have the feeling is the solution. > Permutation looked good, but I am an amateur programer and couldn't quite > grasp the concepts from the examples I found. Dictionaries hold promise as > it can be used to get a count of the letters in words. > > from operator import itemgetter > > def countletters(word): >lettercount = {} >for letter in word: >lettercount[letter] =lettercount.get(letter,0) + 1 >print sorted(lettercount.iteritems(), key=itemgetter(1)) > > > countletters("batty") > [('a', 1), ('y', 1), ('b', 1), ('t', 2)] > > countletters("bat") > [('a', 1), ('b', 1), ('t', 1)] > > countletters("bats") > [('a', 1), ('b', 1), ('s', 1), ('t', 1)] > > bat is in batty. bats is not. > > I have a list of words in wordlist.txt. I can write a loop the do the > letter > counts for each word, but I can't figure out how to compare them. I would actually not bother sorting your return from countletters - keep it a dictionary. Then you can compare like this: mainword = countletters('batty') cmpword = countletters('bat') def myfunc(cmpword, mainword): for letter in cmpword: if mainword.gets(letter): if cmpword[letter] >mainword[letter]: return False else: return False I think that should work. First you're looping over each letter in cmpword. Because mainword is also a dictionary the order isn't terribly important. Then you check if the letter is in mainword. If it's not, obviously cmpword isn't in mainword, so return False. If the letter is, compare the counts. If cmpword has more letters than mainword, it's not in the word so again return False. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
On Sun, 2009-08-23 at 15:06 -0700, kreglet wrote: > Hello, > > The problem that I am having is writing an algorithm for finding all the > possible words from a given word. For example: python > > from "python" you can make the words pot, top, hop, not etc. There are few > examples for making anagrams for a word, but only the whole word. I have > tried unsuccessfully to modify some of these to suit my purpose. Also on the > web there are numerous sites where you can type in a word and it will give > you a list of all the words that could be made from it. How would I go about > this in python? > > thanx, > kreglet Of course doing things by yourself is more exciting and fun (and above all you have the pleasure to learn something new), but if you finally should decide to invoke an external utility (e.g.: for performance issues), then you might be interested in using "an": it's very fast and in the repositories of most linux distro. It has plenty of options. Example usage: an -m4 -w python [search only for single words in the dictionary with a minimum length of 4 letters] returns: hypo phony pony python tony typo Good luck with your project, Mac. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Ptyhon GUI doubt
Hi, I am new to the Python. I have installed Python 2.6.2 version in windows XP. When I try to open the IDLE(Python GUI), getting the below message. Any configuration settings required for this? [cid:675172311@24082009-0830] Please let me know the details. Thanks, Raj <>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
Wayne, The reason I used print sorted is that using just print throws a syntax error: print (lettercount.iteritems(), key=itemgetter(1)) ---> error print lettercount.iteritems(), key=itemgetter(1) ---> error print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works I don't know why. Seems to me that any of the above should work. mainword = countletters('batty') cmpword = countletters('bat') myfunc(cmpword, mainword) Generates error: Traceback (most recent call last): File "/home/kreglet/bin/test.py", line 23, in myfunc(cmpword, mainword) File "/home/kreglet/bin/test.py", line 13, in myfunc for letter in cmpword: TypeError: 'NoneType' object is not iterable mainword = countletters('batty') print mainword returns None cmpword = countletters('bat') print cmpword returns None Both mainword and cmpword are passed to the function but since the values of each are None the function doesn't work. Is this correct? thanx, kreglet Wayne-68 wrote: > > On Sun, Aug 23, 2009 at 10:01 PM, kreglet wrote: > > I would actually not bother sorting your return from countletters - keep > it > a dictionary. > > Then you can compare like this: > > mainword = countletters('batty') > cmpword = countletters('bat') > > def myfunc(cmpword, mainword): > for letter in cmpword: > if mainword.gets(letter): > if cmpword[letter] >mainword[letter]: > return False > else: > return False > > I think that should work. First you're looping over each letter in > cmpword. > Because mainword is also a dictionary the order isn't terribly important. > Then you check if the letter is in mainword. If it's not, obviously > cmpword > isn't in mainword, so return False. If the letter is, compare the counts. > If > cmpword has more letters than mainword, it's not in the word so again > return > False. > > HTH, > Wayne > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://www.nabble.com/Algorithm-tp25107922p25118434.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
Hello Mac, Thanks for the tip. I was aware of an and considered using it. I decided not to use it unless I have no other choice. Although it does exactly what I am after: a) I don't want to use any dependencies other than Python's built in modules b) You hit it right in the nose when you said: "doing things by yourself is more exciting and fun (and above all you have the pleasure to learn something new)" If I did use it I will have learned nothing. The whole purpose of starting this project was so I could learn python. I do so very much enjoy learning new things. The rest of the game is going well. So for I've figured out how to: 1. Create the gui 2. Pick a word from a list 3. Scramble the letters 4. Display them in a drawing area 5. Allow user to click the letters to make a word 6. Check the guessed word against a list of words 7. If matched guessed word is added to a treeview (list box) I am confidant that i can create functions for: 1. Scoring points 2. Advancing to next round 3. Saving the game 4. Polish the gui, etc The only thing I am having problems with (so far) is this algorithm which would let the player know how many words are left to guess. When I am finished this project, I plan to post it somewhere so that others can learn from it too. Peace, kreglet Mac Ryan wrote: > > > Of course doing things by yourself is more exciting and fun (and above > all you have the pleasure to learn something new), but if you finally > should decide to invoke an external utility (e.g.: for performance > issues), then you might be interested in using "an": it's very fast and > in the repositories of most linux distro. It has plenty of options. > Example usage: > > an -m4 -w python [search only for single words in the dictionary with a > minimum length of 4 letters] returns: > hypo > phony > pony > python > tony > typo > > Good luck with your project, > Mac. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://www.nabble.com/Algorithm-tp25107922p25119206.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
On Mon, Aug 24, 2009 at 10:48 AM, kreglet wrote: > > Wayne, > > The reason I used print sorted is that using just print throws a syntax > error: > > print (lettercount.iteritems(), key=itemgetter(1)) ---> error > print lettercount.iteritems(), key=itemgetter(1) ---> error > print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works Ah, excuse me - I just glanced at your function and didn't notice you weren't returning any value. You should be returning lettercount from your function. > > > I don't know why. Seems to me that any of the above should work. > > mainword = countletters('batty') > cmpword = countletters('bat') > > myfunc(cmpword, mainword) > > Generates error: > > Traceback (most recent call last): > File "/home/kreglet/bin/test.py", line 23, in >myfunc(cmpword, mainword) > File "/home/kreglet/bin/test.py", line 13, in myfunc >for letter in cmpword: > TypeError: 'NoneType' object is not iterable > > mainword = countletters('batty') > print mainword returns None > > cmpword = countletters('bat') > print cmpword returns None > > Both mainword and cmpword are passed to the function but since the values > of > each are None > the function doesn't work. > > Is this correct? > That's correct - your function countletters returns None, so you're assigning cmpword to None. And since None is not an iterable type, it throws an error. Try returning a value like I mentioned above and see how that works. HTH, Wayne > > thanx, > kreglet > > > Wayne-68 wrote: > > > > On Sun, Aug 23, 2009 at 10:01 PM, kreglet wrote: > > > > I would actually not bother sorting your return from countletters - keep > > it > > a dictionary. > > > > Then you can compare like this: > > > > mainword = countletters('batty') > > cmpword = countletters('bat') > > > > def myfunc(cmpword, mainword): > > for letter in cmpword: > > if mainword.gets(letter): > > if cmpword[letter] >mainword[letter]: > > return False > > else: > > return False > > > > I think that should work. First you're looping over each letter in > > cmpword. > > Because mainword is also a dictionary the order isn't terribly important. > > Then you check if the letter is in mainword. If it's not, obviously > > cmpword > > isn't in mainword, so return False. If the letter is, compare the counts. > > If > > cmpword has more letters than mainword, it's not in the word so again > > return > > False. > > > > HTH, > > Wayne > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > View this message in context: > http://www.nabble.com/Algorithm-tp25107922p25118434.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
"kreglet" wrote The reason I used print sorted is that using just print throws a syntax error: print (lettercount.iteritems(), key=itemgetter(1)) ---> error print lettercount.iteritems(), key=itemgetter(1) ---> error print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works I don't know why. Seems to me that any of the above should work. Nope, print takes a string (or something that can be converted to a string) as its argument. It does not know what key=itemgetter(1) means. That only makes sense to a sort function. You would need print lettercount.iteritems() Although I'm not sure that would actually print what you expect! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Template long text substitution
Hi Guys, I am trying to do the following, and im not sure how to deal with a blob of text. I have the following file i am using as a template %%NAME%% %%NUMBER%% %%REPORT%% and i have a corresponding file with values name="bob" number="123" report="report is long and spans multiple lines. It also is already in the exact format i want to replace the above template var %%REPORT%% with" The report part is obviously the problem. My code so far reads line by line and splits on the = and reads the values into a dict. file = open('test','r') data = {} for line in file: line = line.strip() line = line.split('=') data[line[0]] = line[1] My intention is then to replace the %%NAME%% with data['name'] etc. If it makes a difference, the report= will be generated by another program and given to me in the exact format i need to substitute it into %%REPORT%%. (or i can put it in that file). I hope this makes sense, thanks in advance - i really do appreciate it :) Stefan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Template long text substitution
Stefan, Have you considered a templating engine such as Jinja or Cheetah? http://jinja.pocoo.org/2/ www.cheetahtemplate.org/ They should be able to handle a lot of the parsing and pairing of variables in a template that you're attempting. Basically, you use template syntax (as you seem to have created on your own), and then simply fill in those variables by passing in a "context", or dictionary of objects. The great thing about these templating engines is that they come pre-packaged with standard looping constructs, so you don't have to reinvent that wheel. I've been tinkering with Jinja and can't say enough about it. HTH, Serdar ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pexpect latest version.
Hello everybody, this is more a request of info than of help. I want to play around with pexpect a bit, but I am confused on what is the latest stable version. On SF (linked from the site of the pexpect developer) http://pexpect.sourceforge.net/pexpect.html it seems the latest version (2.3), but I was able to download the 2.4 from here: http://pypi.python.org/pypi/pexpect/ which is just over one year old. On the other hand the version included in the repos of my linux distro is 2.3.1. So my questions: 1. Does anybody know if both version are compatible with python 2.6? 2. Does anybody know if 2.4 is a stable (secret) version? Of course if you have experience with pexpect and feel compelled to give some preliminary advice (like known limitations or quirks that take hours to understand, or "don't use it because is broken" kind of remarks) I will be glad to hear those as well! :) Many thanks for your time, Mac. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Template long text substitution
Stefan Lesicnik wrote: Hi Guys, I am trying to do the following, and im not sure how to deal with a blob of text. I have the following file i am using as a template %%NAME%% %%NUMBER%% %%REPORT%% and i have a corresponding file with values name="bob" number="123" report="report is long and spans multiple lines. It also is already in the exact format i want to replace the above template var %%REPORT%% with" The report part is obviously the problem. My code so far reads line by line and splits on the = and reads the values into a dict. file = open('test','r') data = {} for line in file: line = line.strip() line = line.split('=') data[line[0]] = line[1] My intention is then to replace the %%NAME%% with data['name'] etc. If it makes a difference, the report= will be generated by another program and given to me in the exact format i need to substitute it into %%REPORT%%. (or i can put it in that file). I hope this makes sense, thanks in advance - i really do appreciate it :) Stefan There's much more left unsaid here. In order to parse that file, you have to know exactly what it will contain. I'll ask a few questions that might help you narrow it down. Currently you have three keywords "name" "number" and "report". Do you want this to be variable, and the order arbitrary? Clearly, you want to be case insensitive, since your template file has different case than the data file. Will there always be quotes around the value? Your current code doesn't check for them, nor does it remove them. But you do have to decide for the third keyword when the text ends. One way could be "till end of file" but I'm guessing you don't want that, especially if order is irrelevant. Another way could be "till another line with an '=' sign occurs". But what happens if you want an equal sign in the text someday? Or you could end when you encounter the next quote mark. But what happens if you want an embedded quote in the substituted text? If you have some choice in the file format, you could change it to specify the keywords with double-percent signs, same as in the template file. Then the rule could be that any line that starts with a percented keyword is the start of a new keyword, and any other line is appended (with newline between) to the previous value. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ptyhon GUI doubt
Hi Raj, I used to get this error sometimes when I was using Windows. Killing the Python process via Task Manager and restarting IDLE was enough to get me going but if it still remains an issue, you can run Python in single process mode, the following is snipped from IDLE help: "Running without a subprocess: If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible. " On Mon, Aug 24, 2009 at 9:28 PM, Reddy Etikela, Rajasekhar wrote: > Hi, > > I am new to the Python. I have installed Python 2.6.2 version in windows XP. > > When I try to open the IDLE(Python GUI), getting the below message. Any > configuration settings required for this? > > > Please let me know the details. > > Thanks, > Raj > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Algorithm
Wayne, > def myfunc(cmpword, mainword): > for letter in cmpword: > if mainword.gets(letter): > if cmpword[letter] >mainword[letter]: > return False > else: > return False I tried your function and couldn't get it to work. It threw an error in the line "if mainword.gets(letter):" saying that "gets" was not an attribute of dictionary. I tried it with "if mainword.get(letter):" -- no s but that would't work either. I've been playing with this most of the day and this is what i came up with: from operator import itemgetter class letters: def __init__(self): self.lettercount={} self.inlist=False self.inword=False self.mainword="" self.cmpword="" lc=letters() def countletters(word): lc.lettercount = {} for letter in word: lc.lettercount[letter] =lc.lettercount.get(letter,0) + 1 print sorted(lc.lettercount.iteritems(), key=itemgetter(1)) lc.mainword="batty" lc.cmpword="batyy" countletters(lc.mainword) mainword = lc.lettercount countletters(lc.cmpword) cmpword = lc.lettercount lst=[] for key in cmpword.keys(): for ky in mainword.keys(): if key<>ky: pass else: lst.append(key) for key in cmpword.keys(): if key not in lst: lc.inlist=False break else: lc.inlist=True # At this point the program stops if a letter is in cmpword that is not in mainword # What I try to do next is compare the values of the dictionary keys( this is where I'm getting confused) # get keys/values from cmpword # compare the values of the keys from mainword that match the keys from cmpword # if there is a key in mainword that is not in cmpword, ignore it # if a key is the same in both compare the values #if the value of a key that is in cmpword is greater the the value of the corresponding key in mainword then cmpword would not be in mainword -- cmpkeys=[] cmpvals=[] if lc.inlist==True: for key, val in cmpword.items(): cmpkeys.append(key) cmpvals.append(val) for a in range(len(cmpkeys)): for ky, vl in mainword.items(): if cmpkeys[a]==ky: #? if cmpvals[a]>vl: # lc.inword=False else: lc.inword=True print cmpkeys[a], cmpvals[a], ky, vl print if lc.inword==True: print lc.cmpword + " is IN: " + lc.mainword else: print lc.cmpword + " is Not in: " + lc.mainword Lol, it aint pretty, and it almost works. If you change the letters in lc.cmpword you'll see what I mean. There's got to be another way :) cheers, kreglet Wayne-68 wrote: > > > > That's correct - your function countletters returns None, so you're > assigning cmpword to None. And since None is not an iterable type, it > throws > an error. Try returning a value like I mentioned above and see how that > works. > > HTH, > Wayne > > -- View this message in context: http://www.nabble.com/Algorithm-tp25107922p25124979.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor