Paste: Starpial - some SPOJ solutions
Author: | KernelJ |
Mode: | text |
Date: | Fri, 8 Jul 2011 19:15:33 |
Plain Text |
Problem TEST
:print :(readline dup "42" =) until
Problem PRIME1
:(primes readint upfrom readint upto :print map; "" print) readint times
(2) 3 buildprimes !primes
((primes' candidate) candidate 2 + buildprimes),
(primes candidate 2 + buildprimes),
primes candidate sqrt upto (candidate p % 0 =), #p exists ?
#primes #candidate !buildprimes
Problem SBSTR1
:(0 1 readitem readitem contains ? print) 24 times
Problem ONP
?\Simple Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ atom | atom
?\ div := exp / exp | exp
?\ mul := div * div | div
?\ sub := mul - mul | mul
?\ term := sub + sub | sub
:(readline parseatom "" print) readint times
:`badchar
:(s[0] write s tail),
(s[0] \a >=), (s[0] \z <=), & ?
:(s tail parseterm \) match)
s[0] \( = ?#
#s @parseatom
s parsesub
:r :(r tail parsesub \+ write), (r[0] \+ =), ?# #r#
#s !parseterm
s parsemul
:r :(r tail parsemul \- write), (r[0] \- =), ?# #r#
#s !parsesub
s parsediv
:r :(r tail parsediv \* write), (r[0] \* =), ?# #r#
#s !parsemul
s parseexp
:r :(r tail parseexp \/ write), (r[0] \/ =), ?# #r#
#s !parsediv
s parseatom
:r :(r tail parseatom \^ write), (r[0] \^ =), ?# #r#
#s !parseexp
`badchar (s tail), (s[0] c =), ?
#s #c !match
Problem ONP
?\Alternative Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ exp | atom
?\ div := exp / div | exp
?\ mul := div * mul | div
?\ sub := mul - sub | mul
?\ term := sub + term | sub
:(readline parseatom "" print) readint times
:`badchar
:(s[0] write s tail),
(s[0] \a >=), (s[0] \z <=), & ?
:(s tail parseterm \) match)
s[0] \( = ?#
#s @parseatom
s parsesub
:r :(r tail parseterm \+ write), (r[0] \+ =), ?# #r#
#s !parseterm
s parsemul
:r :(r tail parsesub \- write), (r[0] \- =), ?# #r#
#s !parsesub
s parsediv
:r :(r tail parsemul \* write), (r[0] \* =), ?# #r#
#s !parsemul
s parseexp
:r :(r tail parsediv \/ write), (r[0] \/ =), ?# #r#
#s !parsediv
s parseatom
:r :(r tail parseexp \^ write), (r[0] \^ =), ?# #r#
#s !parseexp
`badchar (s tail), (s[0] c =), ?
#s #c !match
Problem PALIN
?\Awful solution
:(readint 1 + getpalindrome print) readint times
(n 1 + getpalindrome), n (n .toString dup rev =), ? #n !getpalindrome
Problem PALIN
?\Proper solution
:(readint getpalindrome print) readint times
(n palinify2), (n palinify), (n palinify parseInt n >), ? #n !getpalindrome
( (h palineven), (h palinodd), (s length 2 % 0 =), ?
(h palinodd), (h palineven), (s length 2 % 0 =), ?
h length s takehalf length = ?
s takehalf parseInt 1 + .toString !h n.toString !s
), #n !palinify2
((h palinodd), (h palineven), (s length 2 % 0 =), ?
s takehalf !h n.toString !s), #n !palinify
(h' (h';) rev') #h !palinodd (h' h rev') #h !palineven
s (s length 1 - m / 1 +), take #s !takehalf
Author: | KernelJ |
Mode: | tex |
Date: | Sun, 23 Oct 2011 15:16:07 |
Plain Text |
Problem TEST
:(readline dup "42" =)
:print
until
Problem PRIME1
:(primes readint upfrom readint upto :print map; "" print) readint times
(2) 3 buildprimes !primes
primes candidate sqrt upto (candidate p
(primes candidate 2 + buildprimes),
(primes candidate push candidate 2 + buildprimes),
?
#primes #candidate !buildprimes
Problem SBSTR1
:((readitem readitem contains), 1 0 ? print) 24 times
Problem ONP
?\Simple Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ atom | atom
?\ div := exp / exp | exp
?\ mul := div * div | div
?\ sub := mul - mul | mul
?\ term := sub + sub | sub
:(readline parseatom "" print) readint times
(s[0] \( =),
:(s tail parseterm \) match)
(s[0] \a >=), (s[0] \z <=), &
:(s[0] write s tail),
:`badchar
??#
#s @parseatom
s parsesub
(r[0] \+ =),
:(r tail parsesub \+ write),
:r
?# #r#
#s !parseterm
s parsemul
(r[0] \- =),
:(r tail parsemul \- write),
:r
?# #r#
#s !parsesub
s parsediv
(r[0] \* =),
:(r tail parsediv \* write),
:r
?# #r#
#s !parsemul
s parseexp
(r[0] \/ =),
:(r tail parseexp \/ write),
:r
?# #r#
#s !parsediv
s parseatom
(r[0] \^ =),
:(r tail parseatom \^ write),
:r
?# #r#
#s !parseexp
(s[0] c =),
(s tail),
`badchar
?
#s #c !match
Problem ONP
?\Alternative Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ exp | atom
?\ div := exp / div | exp
?\ mul := div * mul | div
?\ sub := mul - sub | mul
?\ term := sub + term | sub
:(readline parseatom "" print) readint times
s[0] \( =
:(s tail parseterm \) match)
(s[0] \a >=), (s[0] \z <=), &
:(s[0] write s tail),
:`badchar
??#
#s @parseatom
s parsesub
(r[0] \+ =),
:(r tail parseterm \+ write),
:r
?# #r#
#s !parseterm
s parsemul
(r[0] \- =),
:(r tail parsesub \- write),
:r
?# #r#
#s !parsesub
s parsediv
(r[0] \* =),
:(r tail parsemul \* write),
:r
?# #r#
#s !parsemul
s parseexp
(r[0] \/ =),
:(r tail parsediv \/ write),
:r
?# #r#
#s !parsediv
s parseatom
(r[0] \^ =),
:(r tail parseexp \^ write),
:r
?# #r#
#s !parseexp
(s[0] c =),
(s tail),
`badchar
?
#s #c !match
Problem PALIN
?\Awful solution
:(readint 1 + getpalindrome print) readint times
(n .show dup rev =),
n
(n 1 + getpalindrome),
? #n !getpalindrome
Problem PALIN
?\Proper solution
:(readint getpalindrome print) readint times
(n palinify parseInt n >),
(n palinify),
(n palinify2),
? #n !getpalindrome
(
h length s takehalf length =
(s length 2
(s length 2
?
s takehalf parseInt 1 + .show !h
n.show !s
), #n !palinify2
(
(s length 2
s takehalf !h
n.show !s
), #n !palinify
(h' (h';) rev') #h !palinodd
(h' h rev') #h !palineven
s (s length 1 - m / 1 +), take #s !takehalf
Author: | KernelJ |
Mode: | text |
Date: | Sun, 7 Jul 2013 22:30:22 |
Plain Text |
Reference FIB
{1 1 loop[2dup +]}
Problem TEST
until[readline dup "42" =]: print
Problem PRIME1
readint times: primes readint upfrom readint upto each[print] "" print
@primes: {2} 3 loop: #candidate
dup candidate sqrt upto exists[#p candidate p% 0=] if_f: candidate push
candidate 2 +
Problem SBSTR1
24 times: readitem readitem contains 1 0 ? print
Problem ONP
?\Simple Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ atom | atom
?\ div := exp / exp | exp
?\ mul := div * div | div
?\ sub := mul - mul | mul
?\ term := sub + sub | sub
readint times: readline parseatom drop "" print
@parseatom: #{x>xs}
if(x '(' =): xs parseterm \) match
elseif(x 'a' >=, x 'z' <=, &): x write xs
else: `badchar
!match: if(over head =)[tail] else[`badchar]
!parseterm: parsesub dup head '+' = if_t: tail parsesub '+' write
!parsesub : parsemul dup head '-' = if_t: tail parsemul '-' write
!parsemul : parsediv dup head '*' = if_t: tail parsediv '*' write
!parsediv : parseexp dup head '/' = if_t: tail parseexp '/' write
!parseexp : parseatom dup head '^' = if_t: tail parseatom '^' write
Problem ONP
?\Alternative Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ exp | atom
?\ div := exp / div | exp
?\ mul := div * mul | div
?\ sub := mul - sub | mul
?\ term := sub + term | sub
readint times: readline parseatom drop "" print
@parseatom: #{x>xs}
if(x '(' =): xs parseterm \) match
elseif(x 'a' >=, x 'z' <=, &): x write xs
else: `badchar
!match: if(over head =)[tail] else[`badchar]
!parseterm: parsesub dup head '+' = if_t: tail parseterm '+' write
!parsesub: parsemul dup head '-' = if_t: tail parsesub '-' write
!parsemul: parsediv dup head '*' = if_t: tail parsemul '*' write
!parsediv: parseexp dup head '/' = if_t: tail parsediv '/' write
!parseexp: parseatom dup head '^' = if_t: tail parseexp '^' write
Problem ONP
?>Regex Demo<?
?\Grammar
?\ atom := a-z | (term)
?\ exp := atom ^ exp | atom
?\ div := exp / div | exp
?\ mul := div * mul | div
?\ sub := mul - sub | mul
?\ term := sub + term | sub
readint times: readline atom print
@atom:[#/\([term]>t\)/ t]`[#/a-z>a/ a]
!term:{#/[sub]>s\+[term]>t/ s.. t.. '+'}`[#/[sub]>s/ s]#
!sub:{#/[mul]>m\-[sub]>s/ m.. s.. '-'}`[#/[mul]>m/ m]#
!mul:{#/[div]>d\*[mul]>m/ d.. m.. '*'}`[#/[div]>d/ d]#
!div:{#/[exp]>e\/[div]>d/ e.. d.. '/'}`[#/[exp]>e/ e]#
!exp:{#/[atom]>a\^[exp]>e/ a.. e.. '^'}`[#/[atom]>a/ a]#
New Annotation