Skip to content
Browse files

Coding Dojo "Roman Literals"

By the Oslo Perl 6 Patterns hackers
  • Loading branch information...
0 parents commit 7858afb9a7f4731404fb32daf14e3237139e3d09 @moritz committed Apr 22, 2012
Showing with 55 additions and 0 deletions.
  1. +25 −0 lib/Math/Roman.pm
  2. +30 −0 t/sanity.t
25 lib/Math/Roman.pm
@@ -0,0 +1,25 @@
+module Math::Roman;
+
+my @romans = <
+ 500 D
+ 90 XC
+ 100 C
+ 50 L
+ 45 XLV
+ 10 X
+ 5 V
+>;
+
+sub roman($_) is export {
+ for @romans -> $num, $rom {
+ for @romans -> $n, $r {
+ next unless $n.substr(0, 1) eq '1';
+ return "$r$rom" if $num > $n && $_ == $num - $n;
+ };
+ return "I$rom" when $num - 1;
+ return $rom ~ roman($_ - $num) if $_ >= $num;
+ }
+ return 'I' x $_;
+}
+
+#vim:ft=perl6
30 t/sanity.t
@@ -0,0 +1,30 @@
+use Math::Roman;
+use Test;
+
+my @tests = <
+ 1 I
+ 2 II
+ 4 IV
+ 5 V
+ 6 VI
+ 7 VII
+ 9 IX
+ 10 X
+ 14 XIV
+ 15 XV
+ 19 XIX
+ 38 XXXVIII
+ 39 XXXIX
+ 40 XL
+ 45 XLV
+ 46 XLVI
+ 90 XC
+ 99 XCIX
+ 400 CD
+>;
+
+for @tests -> $arab, $roman {
+ is roman($arab), $roman, "roman($arab) is $roman";
+}
+
+done;

0 comments on commit 7858afb

Please sign in to comment.
Something went wrong with that request. Please try again.