Created
July 22, 2012 23:01
-
-
Save arkadijs/3161315 to your computer and use it in GitHub Desktop.
LDN Coding Dojo: Groovy
This file contains hidden or 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
class ArabicNumeralsTest extends GroovyTestCase { | |
def converter = new RomanNumerals() | |
void testOne() { | |
assert 1 == converter.toArabic("I") | |
} | |
void testDigit() { | |
assert 4 == converter.toArabic("IV") | |
assert 5 == converter.toArabic("V") | |
assert 9 == converter.toArabic("IX") | |
assert 10 == converter.toArabic("X") | |
assert 40 == converter.toArabic("XL") | |
assert 50 == converter.toArabic("L") | |
assert 90 == converter.toArabic("XC") | |
assert 100 == converter.toArabic("C") | |
assert 400 == converter.toArabic("CD") | |
assert 500 == converter.toArabic("D") | |
assert 900 == converter.toArabic("CM") | |
assert 1000 == converter.toArabic("M") | |
} | |
void test19() { | |
assert 19 == converter.toArabic("XIX") | |
} | |
void test999() { | |
assert 999 == converter.toArabic("CMXCIX") | |
} | |
void test99() { | |
assert 99 == converter.toArabic("XCIX") | |
} | |
void test1789() { | |
assert 1789 == converter.toArabic("MDCCLXXXIX") | |
} | |
void testIdiot() { | |
assert 0 == converter.toArabic("") | |
} | |
} |
This file contains hidden or 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
class RomanNumerals { | |
def romanDigits = [ | |
1: 'I', | |
5: 'V', | |
10: 'X', | |
50: 'L', | |
100: 'C', | |
500: 'D', | |
1000: 'M', | |
4: 'IV', | |
9: 'IX', | |
40: 'XL', | |
90: 'XC', | |
400: 'CD', | |
900: 'CM' | |
] | |
def reverseRomanKeys = romanDigits.keySet().sort().reverse() | |
def arabicDigits = romanDigits.inject([:]) {map, entry -> | |
map[entry.value] = entry.key | |
map | |
} | |
def toRoman(arabic) { | |
def s = '' | |
reverseRomanKeys.every {key -> | |
while (arabic >= key) { | |
arabic = arabic - key | |
s += romanDigits[key] | |
} | |
arabic | |
} | |
s | |
} | |
def toArabic(String roman) { | |
def n = 0 | |
while (roman.length()) { | |
for (i in 1..0) { | |
if (roman.length() > i && arabicDigits.containsKey(roman[0..i])) { | |
n += arabicDigits[roman[0..i]] | |
roman = roman.substring(1 + i) | |
break | |
} | |
} | |
} | |
n | |
} | |
} |
This file contains hidden or 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
class RomanNumeralsTest extends GroovyTestCase { | |
def converter = new RomanNumerals() | |
void testOne() { | |
assert "I" == converter.toRoman(1) | |
} | |
void testFive() { | |
assert "V" == converter.toRoman(5) | |
} | |
void testDigits() { | |
assert "X" == converter.toRoman(10) | |
assert "L" == converter.toRoman(50) | |
assert "C" == converter.toRoman(100) | |
assert "D" == converter.toRoman(500) | |
assert "M" == converter.toRoman(1000) | |
} | |
void testTwoDigits() { | |
assert 'VI' == converter.toRoman(6) | |
assert 'XI' == converter.toRoman(11) | |
} | |
void testTwoSimilarDigits() { | |
assert 'II' == converter.toRoman(2) | |
} | |
void testThreeSimilarDigits() { | |
assert 'III' == converter.toRoman(3) | |
} | |
void testSeven() { | |
assert 'VII' == converter.toRoman(7) | |
} | |
void test4() { | |
assert 'IV' == converter.toRoman(4) | |
} | |
void test40() { | |
assert 'XL' == converter.toRoman(40) | |
} | |
void test9() { | |
assert 'IX' == converter.toRoman(9) | |
} | |
void testFours() { | |
assert 'CD' == converter.toRoman(400) | |
assert 'CDXL' == converter.toRoman(440) | |
assert 'CDXLIV' == converter.toRoman(444) | |
} | |
void testNines() { | |
assert 'CM' == converter.toRoman(900) | |
assert 'CMXC' == converter.toRoman(990) | |
assert 'CMXCIX' == converter.toRoman(999) | |
} | |
void test1789() { | |
assert 'MDCCLXXXIX' == converter.toRoman(1789) | |
} | |
void test0() { | |
assert '' == converter.toRoman(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment