%!PS % begin included library code % see https://codeberg.org/Firedrake/postscript-libraries/ /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 /reduce { % array proc -> value 2 dict begin /p exch def /a exch def a 0 get 1 1 a length 1 sub { a exch get p } for end } bind def /reverse { 1 dict begin dup length /l exch def [ exch aload pop 2 1 l { -1 roll } 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 /c.isdigit { dup 48 ge exch 57 le and } bind def /s2a { [ exch { } forall ] } bind def /test.start { print (:) print /test.pass 0 def /test.count 0 def } bind def /apop.right { % [a b c] -> [a b] c [ exch aload length 1 add 1 roll ] exch } bind def /map { % array proc -> array 2 dict begin /p exch def [ exch { p } forall ] end } bind def /filter { % array proc(bool) -> array 1 dict begin /p exch def [ exch { dup p not { pop } if } forall ] end } bind def % end included library code /luhnalgorithm { 0 dict begin s2a { c.isdigit } filter { 48 sub} map apop.right /payload exch def reverse /digits exch def 0 2 digits length 1 sub { digits exch 2 copy get 2 mul dup 9 gt { 9 sub } if put } for 10 digits { add } reduce 10 mod sub payload eq end } bind def (luhnalgorithm) test.start (17893729974) luhnalgorithm test (4137 8947 1175 5904) luhnalgorithm test (4137 8974 1175 5904) luhnalgorithm not test test.end