re: Daily Coding Puzzles - Oct 29th - Nov 2nd VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Wednesday Write a program to reverse the digits of a positive integer but without converting it to a string.
 
#! /usr/bin/perl
use warnings;
use strict;

sub rev {
    my ($x) = @_;
    my $r = my $z = 0;
    my $start = 1;
    while ($x) {
        ! ($r *= 10) and $z += $start or undef $start;
        $r += $x % 10;
        $x = ($x - $x % 10) / 10;
    }
    return '0' x ($z - 1) . $r
}

use Test::More tests => 4;
is rev(3), 3;
is rev(123456789), 987654321;
is rev(4444), 4444;
is rev(1000), '0001';
code of conduct - report abuse