Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Challenge: Nearest Palindromic Number

by tilly (Archbishop)
on Feb 03, 2005 at 06:53 UTC ( [id://427539]=note: print w/replies, xml ) Need Help??


in reply to Re: Challenge: Nearest Palindromic Number
in thread Challenge: Nearest Palindromic Number

Try it with $num = 199. The answer should be 202. The answer that you give is 191.
  • Comment on Re^2: Challenge: Nearest Palindromic Number

Replies are listed 'Best First'.
Re^3: Challenge: Nearest Palindromic Number
by Limbic~Region (Chancellor) on Feb 03, 2005 at 13:13 UTC
    tilly,
    For the record, Roy Johnson was the first to produce working code. Unfortunately it was broken during some refactoring. The code that I validated originally was:
    sub nearest { my $num = shift; my $firstlen = int(length($num)/2 + .5); my $first = substr($num, 0, $firstlen); my $back = reverse($first); substr($back, 0, 1, '') if $firstlen > length($num)/2; # Update (again): if higher, check lower and vice-versa: my $one = $first . $back; $first += $one < $num ? 1 : -1; $back = reverse($first); substr($back, 0, 1, '') if $firstlen > length($num)/2; my $two = $first . $back; return abs( $one - $num ) < abs( $two - $num) ? $one : $two; }

    Cheers - L~R

Re^3: Challenge: Nearest Palindromic Number
by Roy Johnson (Monsignor) on Feb 03, 2005 at 12:06 UTC
    I've been refactoring and got my <=> line reversed. Thanks. It's fixed now.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

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

    No recent polls found