!7 static int maxWords = 100000; static int maxWordLength = 10; static int topTenTargetLength = 10; static Map map; static L topTen; p { map = l_persistentTreeMap("Words + count"); topTen = l_persistentList("Top Ten"); botIfMain(); } svoid addWordHits(Collection words) { for (S word : words) { word = shorten(word, maxWordLength); long count = longMultiSet_add(map, word); insertToTopTen(word, count); } } svoid insertToTopTen(S word, long count) { int i = l(topTen); while (i > 0 && count >= toLong(map.get(topTen.get(i-1)))) --i; if (i >= topTenTargetLength) ret; topTen.add(i, word); truncateList(topTen, topTenTargetLength); } svoid printTopTen { printAsciiHeading("TOP TEN"); pnl(map(topTen, f(S s) { s + " - " + map.get(s) })); } answer { addWordHits(words2(s)); printTopTen(); ret "OK"; }