5 ms·
I can't beat this for terseness, but I think I could whip up something close (say 15 lines) in Python (readable) or Perl (would be shorter than Python but more
by projectramo 8y ago
I can't beat this for terseness, but I think I could whip up something close (say 15 lines) in Python (readable) or Perl (would be shorter than Python but more readable than linux).
I guess I am more curious about what Knuth did! Did he just comment it a lot for "literate" code?
As always I feel the Python (or Ruby if I could write it) is the sweet spot between being easy to read and easy to write.
- deleted 8y ago[deleted]
- jedimastert 8y agoI always kind of duck when I see "readable" and "Perl" in the same sentence. You never know when jokes are gonna start flying.
- forinti 8y agoPerl would also be a lot faster than the shell. It just does less work. So if the file is big, you would probably prefer Perl. perl -lne 'map { $words{lc $_}++ } split /\W+/,$_ }{ print "$words{$_} $_" for (sort {$words{$a}<=>$words{$b}} keys %words)'
- wodenokoto 8y ago15 lines? You must be forgetting about the Counter class from collections. Counter(s.split()).most_common(k) I think there’s a XKCD about importing your solution from the standard library.
- jodrellblank 8y ago"import antigravity" - https://xkcd.com/353/ https://xkcd.com/353/
- svat 8y agoKnuth was writing in Pascal, a language that had many limitations (e.g. it didn't even have a proper string type; "array of 15 characters" and "array of 16 characters" were distinct types) -- see Kernighan's article "Why Pascal is not my favourite programming language" from about the same time. So he had to implement a lot of the primitives in the program, and also more complicated data structures like a hash trie in this case, and the point was to demonstrate how to do that without the program becoming unreadable. I'm reading some of Knuth's Pascal programs recently and writing down notes for myself; ended up discussing some of these points here: https://shreevatsa.github.io/tex/program/pooltype/#other-problems-with-pascal https://shreevatsa.github.io/tex/program/pooltype/#other-pro... (A slightly better comparison may be with the source code of the Unix programs (tr, wc, etc), rather than with shell pipelines composing those programs.) Read the original sources for more: https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-1.pdf https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-... https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-2.pdf https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-... http://www.cs.upc.edu/~eipec/pdf/p583-van_wyk.pdf http://www.cs.upc.edu/~eipec/pdf/p583-van_wyk.pdf
- sedachv 8y agoThank you for those links and your post!