%!PS % begin included library code % see https://codeberg.org/Firedrake/postscript-libraries/ /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.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 /reverse { 1 dict begin dup length /l exch def [ exch aload pop 2 1 l { -1 roll } for ] end } bind def /deepeq { 2 dict begin /a exch def /b exch def a type b type eq { a type /dicttype eq { a length b length eq { << a { pop true } forall b { pop true } forall >> true exch { pop dup a exch known { dup b exch known { dup a exch get exch b exch get deepeq not { pop false } if } { false } ifelse } { false } ifelse } forall } { false } ifelse } { a type dup /arraytype eq exch /stringtype eq or { a length b length eq { true 0 1 a length 1 sub { dup a exch get exch b exch get deepeq not { pop false exit } if } for } { false } ifelse } { a b eq } ifelse } ifelse } { false } ifelse 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 /s2a { [ exch { } forall ] } 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 % end included library code /closestpalindrome { 0 dict begin cvi /n exch def /delta -1 def { /q n delta add alloccvs def q dup s2a reverse a2s deepeq { q exit } if /delta delta neg def delta 0 lt { /delta delta 1 sub def } if } loop end } bind def (closestpalindrome) test.start (123) closestpalindrome (121) eq test (2) closestpalindrome (1) eq test (1400) closestpalindrome (1441) eq test (1000) closestpalindrome (999) eq test test.end