Current results of "dictionary word count" programs...

Bryan Sant bryan.sant at gmail.com
Mon Mar 13 13:58:41 MST 2006


On 3/13/06, Jonathan Ellis <jonathan at carnageblender.com> wrote:
> #!/usr/bin/python2.4
>
> import sys
>
> WORDS_FNAME = '/usr/share/dict/words'
> words = dict((line.rstrip(), 0) for line in file(WORDS_FNAME))
>
> source = len(sys.argv) > 1 and file(sys.argv[1]) or sys.stdin
> for line in source:
>     for word in line.strip().split():
>         if not word:
>             continue
>         try:
>             words[word] += 1
>         except KeyError:
>             pass
>
> for word, count in words.iteritems():
>     if count > 0:
>         print word, count

Much better!

Python
------
LOC:  18
Best Time:  1.732
Worst Time:  2.767
Avg. Time:  2.09



More information about the PLUG mailing list