Tuesday, July 12, 2011

Python Exercises - Count the Words

My Python solution to the problem "count the frequency of words from a given input stream" is here at ideone.com.

I started out by using a For loop to send each word from a single line to the dictionary mapping function. Once this was working and proved that the dictionary mapping function worked, I re-factored the code to use the map() function instead of the For loop. Perhaps I could save some time in the mapping function (named wordcounter) by using the .setdefault method of the Dictionary object instead of explicitly checking for a key's existence in the Dictionary every time the function is called.

No comments:

Post a Comment