factor/extra/benchmark/knucleotide/knucleotide.factor

61 lines
1.5 KiB
Factor
Raw Normal View History

2009-11-05 23:22:21 -05:00
USING: kernel locals io io.files splitting strings io.encodings.ascii
hashtables sequences assocs math namespaces prettyprint
math.parser combinators arrays sorting unicode.case ;
IN: benchmark.knucleotide
: float>string ( float places -- string )
swap >float number>string
"." split1 rot
over length over <
[ CHAR: 0 pad-tail ]
[ head ] if "." glue ;
: discard-lines ( -- )
readln
2007-11-25 00:50:12 -05:00
[ ">THREE" head? [ discard-lines ] unless ] when* ;
: read-input ( -- input )
discard-lines
">" read-until drop
2007-11-25 00:50:12 -05:00
CHAR: \n swap remove >upper ;
: tally ( x exemplar -- b )
2009-11-05 23:22:21 -05:00
clone [ [ inc-at ] curry each ] keep ;
: small-groups ( x n -- b )
swap
2010-01-14 13:08:22 -05:00
[ length swap - 1 + iota ] 2keep
2008-12-17 20:28:07 -05:00
[ [ over + ] dip subseq ] 2curry map ;
: handle-table ( inputs n -- )
small-groups
[ length ] keep
H{ } tally >alist
sort-values reverse
[
dup first write bl
second 100 * over / 3 float>string print
] each
2007-11-25 00:50:12 -05:00
drop ;
2009-11-05 23:22:21 -05:00
:: handle-n ( inputs x -- )
inputs x length small-groups :> groups
groups H{ } tally :> b
x b at [ 0 ] unless*
2009-01-30 13:49:22 -05:00
number>string 8 CHAR: \s pad-tail write ;
: process-input ( input -- )
dup 1 handle-table nl
dup 2 handle-table nl
{ "GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT" }
[ [ dupd handle-n ] keep print ] each
2007-11-25 00:50:12 -05:00
drop ;
: knucleotide ( -- )
"resource:extra/benchmark/knucleotide/knucleotide-input.txt"
ascii [ read-input ] with-file-reader
2007-11-25 00:50:12 -05:00
process-input ;
MAIN: knucleotide