%!PS % begin included library code % see https://codeberg.org/Firedrake/postscript-libraries/ /strjoin % [(a) (b) (c)] (j) -> (ajbjc) { 3 dict begin /j exch def dup 0 get /out exch def /first true def { first { pop /first false def } { out j strconcat exch strconcat /out exch def } ifelse } forall out end } bind def /test.start { print (:) print /test.pass 0 def /test.count 0 def } bind def /alloccvs { 2 dict begin /n exch def /a 1 def n dup 0 lt { /a a 1 add def neg } if { dup 10 lt { exit } if /a a 1 add def 10 idiv } loop pop n a string cvs end } bind def /s2a { [ exch { } forall ] } bind def /enumerate.array { 1 dict begin /a exch def [ 0 1 a length 1 sub { [ exch dup a exch get ] } for ] end } bind def /test.end { ( ) print test.count 0 gt { (Passed ) print test.pass (...) cvs print (/) print test.count (...) cvs print ( \() print test.pass 100 mul test.count idiv (...) cvs print (%\)) print (\r\n) print } if } bind def /a2s { 2 dict begin /i exch def i length dup string /o exch def 1 sub 0 exch 1 exch { dup i 3 -1 roll get o 3 1 roll put } for o end } bind def /test { /test.count test.count 1 add def { /test.pass test.pass 1 add def } { ( ) print test.count (....) cvs print (-fail) print } ifelse } bind def /strconcat % (a) (b) -> (ab) { [ 3 -1 roll s2a aload length 2 add -1 roll s2a aload pop ] a2s } bind def % end included library code /groupdigitsum { 0 dict begin /sz exch def /s exch s2a def /n 0 def { [ s enumerate.array { aload pop /c exch def /i exch def /n n c add 48 sub def i s length 1 sub eq i 1 add sz mod 0 eq or { n alloccvs /n 0 def } if } forall ] () strjoin /s exch def s length sz le { exit } if } loop s end } bind def (groupdigitsum) test.start (111122333) 3 groupdigitsum (359) eq test (1222312) 2 groupdigitsum (76) eq test (100012121001) 4 groupdigitsum (162) eq test test.end