Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Challenge: Nearest Palindromic Number

by Limbic~Region (Chancellor)
on Feb 03, 2005 at 13:13 UTC ( [id://427613]=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found