diff -r ecd989eee8bd -r 59779ce322a6 progs/roman.scala --- a/progs/roman.scala Wed Mar 20 21:50:20 2019 +0000 +++ b/progs/roman.scala Sat Jun 22 08:39:52 2019 +0100 @@ -1,6 +1,7 @@ -// Part 1 about Roman Numerals -//============================= +// Replacement Exam: Scala Part +//======================================= +// Roman Digits and Numerals abstract class RomanDigit case object I extends RomanDigit @@ -18,16 +19,25 @@ // 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]] = +//def optionlist[A](xs: List[Option[A]]): Option[List[A]] = + + +// some testcases +//optionlist(List(Some(1), Some(2), Some(3))) // -> Some(List(1, 2, 3)) +//optionlist(List(Some(1), None, Some(3))) // -> None +//optionlist(List()) // -> Some(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. +// a roman numeral (if possible). In both cases, if the conversion +// is not possible, the functions should return None. -def Char2RomanDigit(c: Char): Option[RomanDigit] = +//def Char2RomanDigit(c: Char): Option[RomanDigit] = -def String2RomanNumeral(s: String) : Option[RomanNumeral] = +//def String2RomanNumeral(s: String) : Option[RomanNumeral] = // some test cases @@ -42,108 +52,63 @@ // (3) Write a recursive function RomanNumral2Int that converts a // RomanNumeral into an integer. -def RomanNumeral2Int(rs: RomanNumeral): Int = +//def RomanNumeral2Int(rs: RomanNumeral): Int = // some test cases -RomanNumeral2Int(List(I,I,I,I)) // 4 -RomanNumeral2Int(List(I,V)) // 4 -RomanNumeral2Int(List(V,I)) // 6 -RomanNumeral2Int(List(I,X)) // 9 -RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979 -RomanNumeral2Int(List(M,C,M,X,L,I,V)) // 1944 +//RomanNumeral2Int(List(I,I,I,I)) // 4 +//RomanNumeral2Int(List(I,V)) // 4 +//RomanNumeral2Int(List(V,I)) // 6 +//RomanNumeral2Int(List(I,X)) // 9 +//RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979 +//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 +// a roman numeral) into an integer (if possible). If // this is not possible, the functions should return None. -def String2Int(s: String): Option[Int] = - -// some test cases -String2Int("IIII") // 4 (though invalid roman numeral) -String2Int("IV") // 4 -String2Int("VI") // 6 -String2Int("IX") // 9 -String2Int("MCMLXXIX") // 1979 -String2Int("MCMXLIV") // 1944 -String2Int("") // 0 -String2Int("MDCLXI") // 1661 -String2Int("MMMCMXCIX") // 3999 -String2Int("XLVIII") // 48 -String2Int("MMVIII") // 2008 - -String2Int("MMXI") // 2011 -String2Int("MIM") // 1999 -String2Int("MCMLVI") // 1956 - -String2Int("III") // 3 -String2Int("XXX") // 30 -String2Int("CCC") // 300 -String2Int("MMM") // 3000 -String2Int("VII") // 7 -String2Int("LXVI") // 66 -String2Int("CL") // 150 -String2Int("MCC") // 1200 -String2Int("IV") // 4 -String2Int("IX") // 9 -String2Int("XC") // 90 -String2Int("MDCLXVI") // 1666 - -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 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 -//=========================================== - -// (6) Write a function that validates roman numerals according -// to the rules given in the CW. - -def isValidNumeral(digitList: RomanNumeral): Boolean = +//def String2Int(s: String): Option[Int] = // some test cases -val invalids = List("IXC", "XCX", "IIII", "IIIII", "DD", "VL", - "MIM", "XXCIII", "LXXIIX", "IIIIX", "IIXX", - "ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX") +//String2Int("IIII") // 4 (though invalid roman numeral) +//String2Int("IV") // 4 +//String2Int("VI") // 6 +//String2Int("IX") // 9 +//String2Int("MCMLXXIX") // 1979 +//String2Int("MCMXLIV") // 1944 +//String2Int("") // 0 +//String2Int("MDCLXI") // 1661 +//String2Int("MMMCMXCIX") // 3999 +//String2Int("XLVIII") // 48 +//String2Int("MMVIII") // 2008 -val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI", - "MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III", - "XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC", - "MDCLXVI") - -// (7) Write a recursive function that converts an Integer into a -// a roman numeral. The input will be between 0 and 3999. +//String2Int("MMXI") // 2011 +//String2Int("MIM") // 1999 +//String2Int("MCMLVI") // 1956 -def Int2Roman(n: Int): RomanNumeral = +//String2Int("III") // 3 +//String2Int("XXX") // 30 +//String2Int("CCC") // 300 +//String2Int("MMM") // 3000 +//String2Int("VII") // 7 +//String2Int("LXVI") // 66 +//String2Int("CL") // 150 +//String2Int("MCC") // 1200 +//String2Int("IV") // 4 +//String2Int("IX") // 9 +//String2Int("XC") // 90 +//String2Int("MDCLXVI") // 1666 -// (8) Write a function that reads a text file containing valid and -// invalid roman numerals. Convert all valid roman numerals into integers, -// add them up and produce the result as a roman numeral (using the -// function in (7) +//String2Int("VIV") // 9 (but should be written as IX) +//String2Int("IVX") // 14 (also invalid) -def addvalidromanfile(filename: String) = +// error cases +//String2Int("I I I I") +//String2Int("MC?I") +//String2Int("abc") -// a test case -//addvalidromanfile("roman2.txt") +