Created
February 20, 2013 20:38
-
-
Save oker1/4999384 to your computer and use it in GitHub Desktop.
roman numerals kata
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
import org.scalatest._ | |
class RomanNumbersTest extends FlatSpec with ShouldMatchers with BeforeAndAfterEach { | |
private var numbers: RomanNumbers = _ | |
override def beforeEach() { | |
numbers = new RomanNumbers | |
} | |
"Roman numbers" should "convert I to 1" in { | |
numbers.convert("I") should equal (1) | |
} | |
it should "convert II" in { | |
numbers.convert("II") should equal (2) | |
} | |
it should "convert III" in { | |
numbers.convert("III") should equal (3) | |
} | |
it should "convert IV" in { | |
numbers.convert("IV") should equal (4) | |
} | |
it should "convert V" in { | |
numbers.convert("V") should equal (5) | |
} | |
it should "convert X" in { | |
numbers.convert("X") should equal (10) | |
} | |
it should "convert IX" in { | |
numbers.convert("IX") should equal (9) | |
} | |
it should "convert VI" in { | |
numbers.convert("VI") should equal (6) | |
} | |
it should "convert VII" in { | |
numbers.convert("VII") should equal (7) | |
} | |
it should "convert VIII" in { | |
numbers.convert("VIII") should equal (8) | |
} | |
it should "convert XI" in { | |
numbers.convert("XI") should equal (11) | |
} | |
it should "convert XX" in { | |
numbers.convert("XX") should equal (20) | |
} | |
it should "convert XXX" in { | |
numbers.convert("XXX") should equal (30) | |
} | |
it should "convert L" in { | |
numbers.convert("L") should equal (50) | |
} | |
it should "convert LX" in { | |
numbers.convert("LX") should equal (60) | |
} | |
it should "convert XL" in { | |
numbers.convert("XL") should equal (40) | |
} | |
it should "convert C" in { | |
numbers.convert("C") should equal (100) | |
} | |
it should "convert CC" in { | |
numbers.convert("CC") should equal (200) | |
} | |
it should "convert XC" in { | |
numbers.convert("XC") should equal (90) | |
} | |
it should "convert CX" in { | |
numbers.convert("CX") should equal (110) | |
} | |
it should "convert D" in { | |
numbers.convert("D") should equal (500) | |
} | |
it should "convert CD" in { | |
numbers.convert("CD") should equal (400) | |
} | |
it should "convert DC" in { | |
numbers.convert("DC") should equal (600) | |
} | |
it should "convert M" in { | |
numbers.convert("M") should equal (1000) | |
} | |
it should "convert CM" in { | |
numbers.convert("CM") should equal (900) | |
} | |
it should "convert MC" in { | |
numbers.convert("MC") should equal (1100) | |
} | |
it should "convert MD" in { | |
numbers.convert("MD") should equal (1500) | |
} | |
it should "convert MM" in { | |
numbers.convert("MM") should equal (2000) | |
} | |
it should "convert MIX" in { | |
numbers.convert("MIX") should equal (1009) | |
} | |
it should "convert MCIX" in { | |
numbers.convert("MCIX") should equal (1109) | |
} | |
it should "convert MCMLXXXIV" in { | |
numbers.convert("MCMLXXXIV") should equal (1984) | |
} | |
it should "convert MMMMMCCCLXVII" in { | |
numbers.convert("MMMMMCCCLXVII") should equal (5367) | |
} | |
} | |
class RomanNumbers { | |
def summarize(digits: List[Int]): Int = digits match { | |
case x :: Nil => x | |
case x :: y :: tail if x >= y => x + summarize(y :: tail) | |
case x :: y :: tail if x < y => summarize(y :: tail) - x | |
} | |
def convert(s: String) = summarize(s.toList.map(convertDigit)) | |
private def convertDigit(x: Char): Int = x match { | |
case 'I' => 1 | |
case 'V' => 5 | |
case 'X' => 10 | |
case 'L' => 50 | |
case 'C' => 100 | |
case 'D' => 500 | |
case 'M' => 1000 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment