! Copyright (C) 2008 John Benediktsson ! See http://factorcode.org/license.txt for BSD license USING: arrays assocs kernel io math math.parser namespaces prettyprint sequences splitting sorting ; IN: wp : split-words ( string -- seq ) " " split [ length 0 > ] filter ; : count-items ( assoc seq -- assoc' ) [ swap [ [ 0 or 1+ ] change-at ] keep ] each ; : print-item ( result -- ) dup [ first ] [ second ] bi* [ write " " write ] [ number>string print ] bi* ; : sort-assoc ( assoc -- seq ) [ 2array ] { } assoc>map sort-values reverse ; H{ } clone [ split-words count-items ] each-line sort-assoc [ print-item ] each