# Paste: Starpial - some SPOJ solutions

Author: KernelJ text Fri, 8 Jul 2011 19:15:33
Plain Text |
```Problem TEST
:print :(readline dup "42" =) until

Problem PRIME1
(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

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
:(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
:(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
(n 1 + getpalindrome),  n  (n .toString dup rev =), ? #n !getpalindrome

Problem PALIN
?\Proper solution
(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```

## Annotation: same SPOJ solutions in new edition

Author: KernelJ tex Sun, 23 Oct 2011 15:16:07
Plain Text |
```Problem TEST
:print
until

Problem PRIME1
(2) 3 buildprimes !primes
primes candidate sqrt upto (candidate p % 0 =), #p exists
(primes                candidate 2 +  buildprimes),
(primes candidate push candidate 2 +  buildprimes),
?
#primes #candidate !buildprimes

Problem SBSTR1

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
(s[0] \( =),
:(s tail parseterm \) match)
(s[0] \a >=), (s[0] \z <=), &
:(s[0] write   s tail),
??#
#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),
?
#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
s[0] \( =
:(s tail parseterm \) match)
(s[0] \a >=), (s[0] \z <=), &
:(s[0] write   s tail),
??#
#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),
?
#s #c !match

Problem PALIN
?\Awful solution
(n .show dup rev =),
n
(n 1 + getpalindrome),
? #n !getpalindrome

Problem PALIN
?\Proper solution
(n palinify parseInt n >),
(n palinify),
(n palinify2),
? #n !getpalindrome
(
h length s takehalf length =
(s length 2 % 0 =), (h palineven), (h palinodd), ?
(s length 2 % 0 =), (h palinodd), (h palineven), ?
?
s takehalf parseInt 1 + .show !h
n.show !s
), #n !palinify2
(
(s length 2 % 0 =), (h palineven), (h palinodd), ?
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```

## Annotation: Update

Author: KernelJ text Sun, 7 Jul 2013 22:30:22
Plain Text |
```Reference FIB
{1 1 loop[2dup +]}

Problem TEST

Problem PRIME1
@primes: {2} 3 loop: #candidate
dup candidate sqrt upto exists[#p candidate p% 0=] if_f: candidate push
candidate 2 +

Problem SBSTR1

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
@parseatom: #{x>xs}
if(x '(' =):                   xs parseterm \) match
elseif(x 'a' >=, x 'z' <=, &):  x write xs
!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
@parseatom: #{x>xs}
if(x '(' =):                   xs parseterm \) match
elseif(x 'a' >=, x 'z' <=, &):  x write xs
!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