View Single Post
Old Oct 17th, 2005, 4:47 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Eeep! You're doing it the hard way! Honestly, you don't need to work so hard. Python is very good at making things easy, but it does take some practise to wrap your head round.

I always find the best way to approach any programming problem is to divide it up into pieces. Something like:

1. Split sentence up into words.
2. Encode each word into pig latin.
3. Join sentence back up again.

You can then write out some pseudo-code:
words = split_words(sentence)
words = [piglatin(word) for word in words]
sentence = join_words(words)
Now, instead of one problem, you have three smaller problems. Namely, the functions split_words, piglatin, and join_words. If you keep dividing the problem into smaller and simpler pieces, you'll eventually end up at the answer.

However, I'm lecturing, and this doesn't immediately help you. To count the number of words, I suggest you look into 'bag' data structures. Bags are structures that might be used something like this (note that you'd need to create a Bag class first ):
>>> bag = Bag()
>>> bag.add("banana")
>>> bag.add("apple")
>>> bag.add("banana")
>>> bag.count("apple")
1
>>> bag.count("banana")
2
>>> bag.remove("banana")
>>> bag.count("banana")
1
>>> bag.contents
{"banana" : 1, "apple" : 1}
If you're still stuck, post up again. When you've finished the work, tell us how you did
Arevos is offline   Reply With Quote