-
-
Save FROGGS/2538bd5f6f92283c7f66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
my %numerals = < M 1000 D 500 C 100 L 50 X 10 V 5 I 1 >; | |
sub to_roman( $number ) { | |
my $num = $number; | |
my $str = ''; | |
for %numerals.kv -> $ltr, $val { | |
while $num - $val >= 0 { | |
$str ~= $ltr; $num -= $val | |
} | |
} | |
$num ?? fail "I'm sorry, but I can't translate '$number'" | |
!! $str | |
} | |
use Test; | |
is to_roman(3), "III", "3 gets converted to III"; | |
is to_roman(500), "D", "D is 500"; | |
is to_roman(2013), "MMXIII", "2013 is MMXIII"; | |
ok to_roman(-1) ~~ Failure, "-1 returns Failure"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment