--- a/progs/roman.scala Wed Jan 25 17:23:00 2017 +0000
+++ b/progs/roman.scala Thu Jan 26 01:43:31 2017 +0000
@@ -97,14 +97,21 @@
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.
+// Read in these numerals from the file, convert them into
+// integers and then add them all up.
import io.Source
import scala.util._
// function for reading files:
// Source.fromFile("file_name")("ISO-8859-9")
+
+def addromanfile(filename: String) =
+
+//test case
+addromanfile("roman.txt")
+
+
+// Part 2 about Validation of Roman Numerals
+//===========================================
--- a/progs/roman_sol.scala Wed Jan 25 17:23:00 2017 +0000
+++ b/progs/roman_sol.scala Thu Jan 26 01:43:31 2017 +0000
@@ -1,3 +1,6 @@
+// Part 1 about Roman Numerals
+//=============================
+
abstract class RomanDigit
case object I extends RomanDigit
case object V extends RomanDigit
@@ -9,6 +12,12 @@
type RomanNumeral = List[RomanDigit]
+
+// (1) First write a polymorphic function that recursively
+// transforms a list of options into an option of a list.
+// As soon as a None is inside the list, the result is None.
+// Otherwise produce a list of all Some's appended.
+
def optionlist[A](xs: List[Option[A]]): Option[List[A]] = xs match {
case Nil => Some(Nil)
case x::xs => (x, optionlist(xs)) match {
@@ -18,7 +27,16 @@
}
}
-def char2romandigit(c: Char): Option[RomanDigit] = c match {
+optionlist(List(Some(1), Some(2), Some(3)))
+optionlist(List(Some(1), None, Some(3)))
+optionlist(List())
+
+// (2) Write a function first a function that converts a character
+// into a roman digit (if possible). Then convert a string into
+// a roman numeral (if possible). If this is not possible, the functions
+// should return None.
+
+def Char2RomanDigit(c: Char): Option[RomanDigit] = c match {
case 'I' => Some(I)
case 'V' => Some(V)
case 'X' => Some(X)
@@ -30,8 +48,9 @@
}
def String2RomanNumeral(s: String) : Option[RomanNumeral] =
- optionlist(s.toList.map(char2romandigit))
+ optionlist(s.toList.map(Char2RomanDigit))
+// some test cases
String2RomanNumeral("IIII")
String2RomanNumeral("IV")
String2RomanNumeral("VI")
@@ -40,7 +59,9 @@
String2RomanNumeral("MCMXLIV")
String2RomanNumeral("M C M X L I V") // None
-////////////////
+
+// (3) Write a recursive function RomanNumral2Int that converts a
+// RomanNumeral into an integer.
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match {
case Nil => 0
@@ -59,6 +80,7 @@
case I::r => 1 + RomanNumeral2Int(r)
}
+// some test cases
RomanNumeral2Int(List(I,I,I,I)) // 4
RomanNumeral2Int(List(I,V)) // 4
RomanNumeral2Int(List(V,I)) // 6
@@ -67,6 +89,10 @@
RomanNumeral2Int(List(M,C,M,X,L,I,V)) // 1944
+// (4) Write a function that converts a string (containing
+// a roman numeral) into an integer (if possible). If not
+// this is not possible, the functions should return None.
+
def String2Int(s: String): Option[Int] =
String2RomanNumeral(s).map(RomanNumeral2Int(_))
@@ -106,13 +132,34 @@
String2Int("CIM") // ?? 899
String2Int("MDCLXVI") // 1666
-String2Int("VIV") //9, but should be written as IX
+String2Int("VIV") // 9, but should be written as IX
String2Int("IVX") // 14 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.
+
+import io.Source
+import scala.util._
+
+// function for reading files:
+// Source.fromFile("file_name")("ISO-8859-9")
+
+def addromanfile(filename: String) = {
+ val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim)
+ lines.map(String2Int(_)).flatten.sum
+}
+
+
+addromanfile("roman.txt")
+
+
+
+
def runsAllowed(r: RomanDigit) = r match {
case I | X | C | M => true