From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, FREEMAIL_REPLY autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!cyclone.bc.net!newsfeed.telusplanet.net!newsfeed.telus.net!xmission!nnrp.xmission!adam.ruth From: Adam Ruth Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: Thu, 23 Sep 2004 15:13:56 -0600 Organization: InterCation, inc. Message-ID: References: <11b4d.3849$d5.30042@newsb.telia.net> NNTP-Posting-Host: 166.70.154.50 X-Trace: news.xmission.com 1095974037 436 166.70.154.50 (23 Sep 2004 21:13:57 GMT) X-Complaints-To: abuse@xmission.com NNTP-Posting-Date: Thu, 23 Sep 2004 21:13:57 +0000 (UTC) User-Agent: MT-NewsWatcher/3.2 (PPC Mac OS X) Xref: g2news1.google.com comp.lang.ada:4047 Date: 2004-09-23T15:13:56-06:00 List-Id: In article , jayessay wrote: > kevin.cline@gmail.com (Kevin Cline) writes: > > Here is a quick hack Common Lisp version. It is 11 "lines". You > could get rid of a couple more > > (defun word-count (file-spec &key (wordpat (compile-pattern "[A-Za-z]+"))) > (let ((words->count (make-hash-table :test #'equal :size 10007))) > (with-open-file (in file-spec :direction :input) > (loop for line = (read-line in nil nil) > while line > do (replace-match > :source line :target wordpat :every-occurance t > :replacement #'(lambda (word) > (incf (gethash word words->count 0)) "")))) > (format t "~{~A~^~%~}" > (subseq (sort (all (w c) :in words->count) #'> :key #'second) 0 10)))) > I've come to this thread late, so I'm not completely sure about the exact specs of the code, but here's my take on a Python version. It comes in at a svelt 10 lines, and I think it's quite readable. I'm sure I'm missing something though. import sys words = {} for line in sys.stdin.readlines(): for word in line.split(): words[word] = words.get(word, 0) + 1 words = [[x[1], x[0]] for x in words.items()] words.sort() words.reverse() for word in words[:10]: print word[0], word[1] -- Adam Ruth adamruth at mac dot com