Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created May 1, 2013 16:21
Show Gist options
  • Save tadzik/5496321 to your computer and use it in GitHub Desktop.
Save tadzik/5496321 to your computer and use it in GitHub Desktop.
use Test;
my %steps =
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
1 => 'I',
;
sub roman($n) {
return '' if $n == 0;
for %steps.keys.sort({ $^b <=> $^a }) -> $i {
if $n >= $i {
return %steps{$i} ~ roman($n - $i)
}
}
}
my %tests =
1 => 'I',
2 => 'II',
3 => 'III',
4 => 'IV',
5 => 'V',
6 => 'VI',
7 => 'VII',
8 => 'VIII',
9 => 'IX',
10 => 'X',
11 => 'XI',
18 => 'XVIII',
19 => 'XIX',
20 => 'XX',
40 => 'XL',
41 => 'XLI',
42 => 'XLII',
50 => 'L',
51 => 'LI',
52 => 'LII',
89 => 'LXXXIX',
90 => 'XC',
91 => 'XCI',
;
for %tests.keys.sort -> $t {
is roman($t), %tests{$t}
}
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment