Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Challenge: Nearest Palindromic Number

by halley (Prior)
on Feb 02, 2005 at 19:21 UTC ( [id://427381]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Nearest Palindromic Number

Is there something I'm missing here? Just yank the first half of the number in question, reverse THAT, and replace the last half of the number with that. Then incrementation or decrementation is easy, to find the alternative choice.

While not the whole answer, solutions (such as the quick Roy Johnson's) would use code similar to this:

sub palindrate { my $number = shift . ''; my $front = substr($number, 0, (length($number)+1)/2); my $back = reverse($front); chop($front) if not (length($front) % 2); return $front . $back; }

Edge cases I can think of:

  • input is already palindromic (input is answer),
  • higher alternative needs more digits (use all-nines),
  • lower alternative needs fewer digits (use all-nines, minus a digit)
  • is '-12321-' a valid palindromic answer?

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: Challenge: Nearest Palindromic Number
by Limbic~Region (Chancellor) on Feb 02, 2005 at 19:23 UTC
    halley,
    Is there something I'm missing here?

    N = 1000, X = 999
    N = 1085, X = 1111

    Cheers - L~R

      Hrm. That's just an edge case, and will only happen when you find that the palindrate($N) is greater than $N. In those cases, '9'x(length($N)-1) would exceed the decrimented '0990' value.

      I'm still thinking out loud at this stage, but it doesn't seem like you'd have to iterate at all, to find provably close palindromic numbers.

      --
      [ e d @ h a l l e y . c c ]

        Howdy!

        sub palindrate { my $X = shift; my $front = substr($X, 0, (length($X)+1)/2); my $back = reverse $front; substr($back, 0, 1) = '' if length($X) % 2; my $front2 = $front+1; my $back2 = reverse $front2; substr($back2, 0, 1) = '' if length($X) % 2; my $N1 = $front.$back; my $N2 = $front2.$back2; return (abs($X-$N1) < abs($X-$N2)) ? $N1 : $N2; }

        Update: fixed typo in code so it really does work as coded now. The algorithm was correct; the implementation was flawed...and use warnings would have pointed it right out to me...

        Further update: *busted*...It's still not quite right... 91 breaks... ...but I'm gonna leave the code as it stands

        yours,
        Michael
        halley,
        Forgive my hasty initial response. I knew your method was flawed but gave a bad test case. 999 and 1001 are both valid when the input is 1000. Your method fails on the updated test case though 1085. The correct answer should be 1111.

        Cheers - L~R

      Howdy!

      ...yeah, but 1001 is equally valid as an answer...

      yours,
      Michael
        herveus,
        I knew the solution was wrong, but my test data to prove it was flawed. Updated with correct data to show the solution as flawed.

        Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://427381]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found