Paste: pair-anagrams

Author: mrjbq7
Mode: factor
Date: Tue, 16 Apr 2013 22:28:57
Plain Text |
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 ;

Annotation: alice

Author: mrjbq7
Mode: factor
Date: Tue, 16 Apr 2013 22:29:15
Plain Text |
USING: http.client urls urls.secure ;

MEMO: alice-text ( -- string )
    URL" http://thumb.tk/8Qd3" http-get nip ;

: alice-anagrams ( -- seq )
    alice-text pair-anagrams all-longest ;

New Annotation

Summary:
Author:
Mode:
Body: