Skip to content

Instantly share code, notes, and snippets.

@FROGGS

FROGGS/ROMAN.pl Secret

Created June 19, 2013 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/2538bd5f6f92283c7f66 to your computer and use it in GitHub Desktop.
Save FROGGS/2538bd5f6f92283c7f66 to your computer and use it in GitHub Desktop.
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