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.119 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.118 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.117 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.116 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.115 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.114 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.113 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.111 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.110 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.109 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.108 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.107 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm
trackler-2.2.1.106 tracks/perl5/exercises/all-your-base/.meta/solutions/AllYourBase.pm