[Tutor] Algorithm

kreglet kreglet at gmail.com
Mon Aug 24 05:01:34 CEST 2009


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" <kreglet at gmail.com> 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 at 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.



More information about the Tutor mailing list