sub build { my ($f, $l) = @_; my $b = reverse $f; substr($b, 0, 1, '') if $l; return $f . $b; } sub palindrate { my $num = shift; my $is_odd = length($num) % 2; my $first = substr( $num, 0, int(length($num)/2 + .5 ) ); my $one = build( $first, $is_odd ); $first += $one < $num ? 1 : -1; my $two = build( $first, $is_odd ); return ((abs($one-$num) < abs($two-$num)) ? $one : $two); }
Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
In reply to Re^2: Challenge: Nearest Palindromic Number
by dragonchild
in thread Challenge: Nearest Palindromic Number
by Limbic~Region
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |