Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,8 @@
],
"prerequisites": [
"conditionals",
"booleans"
"booleans",
"combinators"
],
"difficulty": 3
},
Expand Down
27 changes: 5 additions & 22 deletions exercises/practice/armstrong-numbers/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
USING: assocs kernel locals math math.functions math.order sequences vectors ;
USING: kernel locals math math.functions math.parser sequences ;
IN: armstrong-numbers

:: digits ( x -- digits )
x 10 /mod :> x! :> xs!
V{ } clone :> digits
x digits push

[ xs 0 = not ]
[
xs 10 /mod x! xs!
x digits push
] while

digits reverse ;

:: powsum ( x -- powsum )
x digits :> digs
digs length :> len

digs [ len ^ ] map sum ;

: armstrong? ( n -- ? )
[ ] [ powsum ] bi = ;
:: armstrong? ( n -- ? )
n number>string [ CHAR: 0 - ] map :> digits
digits length :> len
digits [ len ^ ] map-sum n = ;
33 changes: 8 additions & 25 deletions exercises/practice/gigasecond/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
USING: accessors calendar kernel math math.parser sequences
splitting ;
USING: calendar combinators formatting kernel math.parser sequences splitting ;
IN: gigasecond

<PRIVATE

: pad2 ( n -- str ) number>string 2 CHAR: 0 pad-head ;

! Parse "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SS" into a timestamp.
:: parse-moment ( str -- timestamp )
str "T" split1 :> ( datepart timepart )
datepart "-" split [ string>number ] map first3 :> ( y mo d )
timepart [ "00:00:00" ] unless* ":" split [ string>number ] map
first3 :> ( h mi s )
y mo d h mi s instant <timestamp> ;

:: format-moment ( ts -- str )
ts year>> number>string
ts month>> pad2 "-" glue
ts day>> pad2 "-" glue
ts hour>> pad2 "T" glue
ts minute>> pad2 ":" glue
ts second>> >integer pad2 ":" glue ;

PRIVATE>

: gigasecond-after ( moment -- moment )
parse-moment 1000000000 seconds time+ format-moment ;
"T" split1
[ "-" split [ string>number ] map first3 ]
[ "00:00:00" or ":" split [ string>number ] map first3 ]
bi*
instant <timestamp> 1000000000 seconds time+
[ >date< ] [ >time< ] bi
"%04d-%02d-%02dT%02d:%02d:%02d" sprintf ;
9 changes: 2 additions & 7 deletions exercises/practice/leap/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
USING: combinators kernel math ;
USING: combinators kernel math.functions ;
IN: leap

: leap-year? ( year -- ? )
{
{ [ dup 400 mod zero? ] [ drop t ] }
{ [ dup 100 mod zero? ] [ drop f ] }
{ [ dup 4 mod zero? ] [ drop t ] }
[ drop f ]
} cond ;
[ 4 divisor? ] [ 100 divisor? not ] [ 400 divisor? ] tri or and ;
30 changes: 8 additions & 22 deletions exercises/practice/word-count/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
USING: assocs hashtables kernel locals math sequences splitting
strings unicode ;
USING: assocs fry kernel sequences splitting unicode ;
IN: word-count

: word-char? ( ch -- ? )
dup Letter? over digit? or swap CHAR: ' = or ;

:: strip-apostrophes ( str -- str' )
str dup empty? not [
dup first CHAR: ' = [ rest ] when
dup empty? not [ dup last CHAR: ' = [ but-last ] when ] when
] when ;

:: count-words ( sentence -- counts )
H{ } clone :> result
sentence >lower
[ word-char? not ] split-when
[ empty? not ] filter
[ strip-apostrophes ] map
[ empty? not ] filter
[| word |
word result at 0 or 1 + word result set-at
] each
result ;
: count-words ( sentence -- counts )
>lower
[ [ CHAR: ' = ] [ digit? ] [ Letter? ] tri or or not ] split-when
[ [ CHAR: ' = ] trim ] map
harvest
H{ } clone
[ '[ _ inc-at ] each ] keep ;
Loading