Sha256: 5efc579331ba01f1b2b747a36a138f28ec0631e152280ff02a5b1f19b3280dff

Contents?: true

Size: 636 Bytes

Versions: 73

Compression:

Stored size: 636 Bytes

Contents

package AllYourBase;

use strict;
use warnings;

use Carp;

sub convert_base {
    my( $digits, $in_base, $out_base ) = @_;

    croak "base must be greater than 1" if grep { $_ <= 1 } $in_base, $out_base;

    croak 'negative digit not allowed' if grep { $_ < 0 } @$digits;

    croak 'digit equal of greater than the base' if grep { $_ >= $in_base } @$digits;

    my $number = 0;
    $number = $number * $in_base + $_ for @$digits;

    my @output;

    while( $number ) {
        unshift @output, $number % $out_base;
        $number = int $number / $out_base;
    }

    push @output, 0 unless @output;

    return \@output;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.179 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.178 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.177 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.176 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.175 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.174 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.173 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.172 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.171 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.170 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.169 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.167 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.166 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.165 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.164 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.163 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.162 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.161 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.160 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm