3 ms·
This is how I do this sort of thing all the time. But, the sort is O(n log n), so it's asymptotically less satisfying.
by epistasis 13y ago
This is how I do this sort of thing all the time. But, the sort is O(n log n), so it's asymptotically less satisfying.
- kephra 13y agoTrue, a bag of words would perform better. man bash | tr '[:upper:] ' '[:lower:]\n' | awk '/./ { bag[$1]++ } END { for(word in bag) { print bag[word], word } }' | sort -rn | awk '{ print } NR>=10 { exit(0) }' But it would require more typing and thinking. sure one could do this in awk completely, man bash | awk ' /./ { for (i = 1; i<=NF; i++) { bag[tolower($i)]++ } } END { for (i = 1; i<=10; i++) { score=0; for(word in bag) { if (bag[word] > score) { score=bag[word]; best=word } } printf "%s ", best delete bag[best] } printf "\n" }' if the requirement is: please chose one language and not the complete Unix babylon.