Skip to content

Instantly share code, notes, and snippets.

@masak
Created November 1, 2012 21:08
Show Gist options
  • Select an option

  • Save masak/3996552 to your computer and use it in GitHub Desktop.

Select an option

Save masak/3996552 to your computer and use it in GitHub Desktop.

Revisions

  1. Carl Mäsak created this gist Nov 1, 2012.
    24 changes: 24 additions & 0 deletions base.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    use v6;

    sub base(@rad, @a) { [+] ([\*] 1, @rad.reverse) Z* @a.reverse };

    sub unbase(@rad, $val is copy) {
    reverse gather {
    for @rad.reverse {
    take $val % $_;
    last unless ($val /= $_).=Int;
    }
    take $val if $val;
    }
    }

    use Test;

    is base([10, 10, 10, 10], <4 1 2 3>), 4123;
    is base([0, 3, 12], <3 0 1>), 109;
    is base([1736, 3, 12], <3 0 1>), 109;

    is unbase(<10 10 10 10>, 1234), <1 2 3 4>;
    is unbase(<24 60 60>, 12345), <3 25 45>;
    is unbase(<24 60 60>, 123456), <1 10 17 36>;
    is unbase(<24 60 60>, 3), 3;