DEV Community

Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd

Collapse
 
choroba profile image
E. Choroba
#! /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';