USING: anagrams arrays ascii assocs fry io io.encodings.ascii io.files io.pathnames kernel math math.statistics sequences sequences.extras sets sorting splitting vectors ; IN: pair-anagrams ! http://www.thumbtack.com/engineering/pycon-2013-coding-challenge-roundup/ : split-words ( string -- seq ) [ Letter? not ] split-when ; : candidates ( string -- seq ) >lower split-words members [ length 3 > ] filter ; : unique-pairs ( seq -- pairs ) members dup [ 1 + tail-slice [ 2array ] with map ] with map-index concat members ; : unique? ( elt seq -- ? ) swap first2 '[ first2 [ [ _ = ] [ _ = ] bi or ] either? ] any? ; : push-unique-at ( value key assoc -- ) [ 2dup unique? [ nip ] [ ?push ] if ] change-at ; : collect-unique ( seq quot -- hashtable ) [ dup ] prepose [ push-unique-at ] sequence>hashtable ; inline : pair-anagrams ( string -- seq ) candidates unique-pairs [ concat natural-sort ] collect-unique [ members ] assoc-map [ nip length 1 > ] assoc-filter values ;