diff -r d39b8733c6ea -r 293ea84d82ca progs/roman_sol.scala --- a/progs/roman_sol.scala Thu Jan 26 01:43:31 2017 +0000 +++ b/progs/roman_sol.scala Fri Jan 27 14:55:56 2017 +0000 @@ -81,7 +81,7 @@ } // some test cases -RomanNumeral2Int(List(I,I,I,I)) // 4 +RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid roman number) RomanNumeral2Int(List(I,V)) // 4 RomanNumeral2Int(List(V,I)) // 6 RomanNumeral2Int(List(I,X)) // 9 @@ -96,8 +96,8 @@ def String2Int(s: String): Option[Int] = String2RomanNumeral(s).map(RomanNumeral2Int(_)) - -String2Int("IIII") // 4 invalid +// some test cases +String2Int("IIII") // 4 (though invalid roman numeral) String2Int("IV") // 4 String2Int("VI") // 6 String2Int("IX") // 9 @@ -112,10 +112,6 @@ String2Int("MMXI") // 2011 String2Int("MIM") // 1999 String2Int("MCMLVI") // 1956 -String2Int("XXCIII") // ?? 103 / 83 -String2Int("LXXIIX") // ?? 80 / 78 -String2Int("IIIIX") // ?? 12 invalid -String2Int("IIXX") // ?? 20 / 18 invalid String2Int("III") // 3 String2Int("XXX") // 30 @@ -128,17 +124,16 @@ String2Int("IV") // 4 String2Int("IX") // 9 String2Int("XC") // 90 -String2Int("ICM") // ?? 901 -String2Int("CIM") // ?? 899 String2Int("MDCLXVI") // 1666 -String2Int("VIV") // 9, but should be written as IX -String2Int("IVX") // 14 invalid +String2Int("VIV") // 9 (but should be written as IX) +String2Int("IVX") // 14 (also invalid) // error cases String2Int("MC?I") String2Int("abc") + // (5) The file roman.txt contains a list of roman numerals. // Read in these numerals, convert them into integers and then // add them all up. @@ -158,26 +153,111 @@ addromanfile("roman.txt") +// Part 2 about Validation of Roman Numerals +//=========================================== - +def Digit2Int(r: RomanDigit) = r match { + case I => 1 + case V => 5 + case X => 10 + case L => 50 + case C => 100 + case D => 500 + case M => 1000 +} def runsAllowed(r: RomanDigit) = r match { case I | X | C | M => true case V | L | D => false } -def norunsAllowed(r: RomanDigit) = !runsAllowed(r) - +def subtractable(r1: RomanDigit, r2: RomanDigit) = (r1, r2) match { + case (I, V) | (I, X) => true + case (X, L) | (X, C) => true + case (C, D) | (C, M) => true + case _ => false +} -def isValidNumeral(digitList: RomanNumeral): Bool = digitList match { +def isValidNumeral(digitList: RomanNumeral): Boolean = digitList match { - // empty list is valid + // empty list is valid case Nil => true - // a following digit that is equal or larger is an error - case d1::d2::_ if (d1 <= d2) => false + // no more than three runnables in succession + case d1::d2::d3::d4::_ if (d1 == d2 && d1 == d3 && d1 == d4 && runsAllowed(d1)) => false + + // no more than one non-runnables in succession + case d1::d2::_ if (d1 == d2 && !runsAllowed(d1)) => false + + // subtractable + case d1::d2::_ if (Digit2Int(d1) < Digit2Int(d2) && !subtractable(d1, d2)) => false + + // followable1 + case d1::d2::d3::_ if (Digit2Int(d2) < Digit2Int(d3) && subtractable(d2,d3) && + Digit2Int(d1) < Digit2Int(d2) * 10) => false + + // followable2 + case d1::d2::d3::_ if (Digit2Int(d1) < Digit2Int(d2) && subtractable(d1,d2) && + Digit2Int(d1) <= Digit2Int(d3)) => false // A single digit is always allowed case _::ds => isValidNumeral(ds) } + + +val invalids = List("IXC", "XCX", "IIII", "IIIII", "DD", "VL", "MIM", "XXCIII", "LXXIIX", "IIIIX", + "IIXX", "ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX") +invalids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_)) +invalids.map(String2RomanNumeral(_)).flatten.map((r) => (r, isValidNumeral(r))) + + +val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI", + "MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III", + "XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC", + "MDCLXVI") +valids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_)) + + + + + +def Int2Roman(n: Int): RomanNumeral = n match { + case 0 => Nil + case n if (1000 <= n) => M :: Int2Roman(n - 1000) + case n if (900 <= n) => C :: M :: Int2Roman(n - 900) + case n if (500 <= n) => D :: Int2Roman(n - 500) + case n if (400 <= n) => C :: D :: Int2Roman(n - 400) + case n if (100 <= n) => C :: Int2Roman(n - 100) + case n if (90 <= n) => X :: C :: Int2Roman(n - 90) + case n if (50 <= n) => L :: Int2Roman(n - 50) + case n if (40 <= n) => X :: L :: Int2Roman(n - 40) + case n if (10 <= n) => X :: Int2Roman(n - 10) + case n if (9 <= n) => I :: X :: Int2Roman(n - 9) + case n if (5 <= n) => V :: Int2Roman(n - 5) + case n if (4 <= n) => I :: V :: Int2Roman(n - 4) + case n if (1 <= n) => I :: Int2Roman(n - 1) +} + + +Int2Roman(4) +Int2Roman(6) +Int2Roman(9) +Int2Roman(1979) +Int2Roman(1944) + +RomanNumeral2Int(List(I,V)) +RomanNumeral2Int(List(V,I)) +RomanNumeral2Int(List(I,X)) +RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) +RomanNumeral2Int(List(M,C,M,X,L,I,V)) + + +def addvalidromanfile(filename: String) = { + val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim) + val ints = lines.map(String2RomanNumeral(_)).flatten.filter(isValidNumeral(_)).map(RomanNumeral2Int(_)) + ints.sum +} + + +addvalidromanfile("roman2.txt")