exercise: AllYourBase version: 3 plan: 23 imports: '&convert-base' tests: |- for $c-data.values -> $case { sub call-convert-base { convert-base( bases => %( Z=> .), digits => ., ) given $case; } given $case { if . { throws-like {call-convert-base}, Exception, .; } else { cmp-ok call-convert-base, ‘~~’, |.; } } } unit: module example: |- sub convert-base ( :%bases! where all(.keys ~~ .Set, .values.all > 1), :@digits! where %bases > .all ~~ UInt:D, --> Array[UInt:D] ) is export { from-decimal %bases, to-decimal(%bases, @digits); } sub to-decimal ($input-base, @input-digits) { my $elems = @input-digits.elems; $_ == 0 ?? $elems-- !! last for @input-digits; (loop (my $i = 0; $i < $elems; $i++) { @input-digits.reverse[$i] * $input-base ** $i; }).sum; } sub from-decimal ($output-base, $num is copy) { my UInt:D @output-digits; while $num >= $output-base { unshift @output-digits, $num % $output-base; $num div= $output-base; } @output-digits.unshift: $num; } stub: |- sub convert-base (:%bases!, :@digits!) is export { }