author | Christian Urban <urbanc@in.tum.de> |
Fri, 30 Nov 2018 08:56:22 +0000 | |
changeset 225 | 56732dbefcff |
parent 109 | 293ea84d82ca |
child 265 | 59779ce322a6 |
permissions | -rw-r--r-- |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
1 |
// Part 1 about Roman Numerals |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
2 |
//============================= |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
3 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
4 |
|
104
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
abstract class RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case object I extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case object V extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case object X extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object L extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case object C extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object D extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case object M extends RomanDigit |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
13 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
14 |
type RomanNumeral = List[RomanDigit] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
15 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
16 |
// (1) First write a polymorphic function that recursively |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
17 |
// transforms a list of options into an option of a list. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
18 |
// As soon as a None is inside the list, the result is None. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
19 |
// Otherwise produce a list of all Some's appended. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
20 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
21 |
def optionlist[A](xs: List[Option[A]]): Option[List[A]] = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
22 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
23 |
// (2) Write a function first a function that converts a character |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
24 |
// into a roman digit (if possible). Then convert a string into |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
25 |
// a roman numeral (if possible). If this is not possible, the functions |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
26 |
// should return None. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
27 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
28 |
def Char2RomanDigit(c: Char): Option[RomanDigit] = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
29 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
30 |
def String2RomanNumeral(s: String) : Option[RomanNumeral] = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
31 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
32 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
33 |
// some test cases |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
34 |
//String2RomanNumeral("IIII") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
35 |
//String2RomanNumeral("IV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
36 |
//String2RomanNumeral("VI") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
37 |
//String2RomanNumeral("IX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
38 |
//String2RomanNumeral("MCMLXXIX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
39 |
//String2RomanNumeral("MCMXLIV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
40 |
//String2RomanNumeral("M C M X L I V") // None |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
41 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
42 |
// (3) Write a recursive function RomanNumral2Int that converts a |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
43 |
// RomanNumeral into an integer. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
44 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
45 |
def RomanNumeral2Int(rs: RomanNumeral): Int = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
46 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
47 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
48 |
// some test cases |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
49 |
RomanNumeral2Int(List(I,I,I,I)) // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
50 |
RomanNumeral2Int(List(I,V)) // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
51 |
RomanNumeral2Int(List(V,I)) // 6 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
52 |
RomanNumeral2Int(List(I,X)) // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
53 |
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
54 |
RomanNumeral2Int(List(M,C,M,X,L,I,V)) // 1944 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
55 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
56 |
// (4) Write a function that converts a string (containing |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
57 |
// a roman numeral) into an integer (if possible). If not |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
58 |
// this is not possible, the functions should return None. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
59 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
60 |
def String2Int(s: String): Option[Int] = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
61 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
62 |
// some test cases |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
63 |
String2Int("IIII") // 4 (though invalid roman numeral) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
64 |
String2Int("IV") // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
65 |
String2Int("VI") // 6 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
66 |
String2Int("IX") // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
67 |
String2Int("MCMLXXIX") // 1979 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
68 |
String2Int("MCMXLIV") // 1944 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
69 |
String2Int("") // 0 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
70 |
String2Int("MDCLXI") // 1661 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
71 |
String2Int("MMMCMXCIX") // 3999 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
72 |
String2Int("XLVIII") // 48 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
73 |
String2Int("MMVIII") // 2008 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
74 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
75 |
String2Int("MMXI") // 2011 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
76 |
String2Int("MIM") // 1999 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
77 |
String2Int("MCMLVI") // 1956 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
78 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
79 |
String2Int("III") // 3 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
80 |
String2Int("XXX") // 30 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
81 |
String2Int("CCC") // 300 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
82 |
String2Int("MMM") // 3000 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
83 |
String2Int("VII") // 7 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
84 |
String2Int("LXVI") // 66 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
85 |
String2Int("CL") // 150 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
86 |
String2Int("MCC") // 1200 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
87 |
String2Int("IV") // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
88 |
String2Int("IX") // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
89 |
String2Int("XC") // 90 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
90 |
String2Int("MDCLXVI") // 1666 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
91 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
92 |
String2Int("VIV") // 9 (but should be written as IX) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
93 |
String2Int("IVX") // 14 (also invalid) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
94 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
95 |
// error cases |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
96 |
String2Int("MC?I") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
97 |
String2Int("abc") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
98 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
99 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
100 |
// (5) The file roman.txt contains a list of roman numerals. |
108 | 101 |
// Read in these numerals from the file, convert them into |
102 |
// integers and then add them all up. |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
103 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
104 |
import io.Source |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
105 |
import scala.util._ |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
106 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
107 |
// function for reading files: |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
108 |
// Source.fromFile("file_name")("ISO-8859-9") |
108 | 109 |
|
110 |
def addromanfile(filename: String) = |
|
111 |
||
112 |
//test case |
|
113 |
addromanfile("roman.txt") |
|
114 |
||
115 |
||
116 |
// Part 2 about Validation of Roman Numerals |
|
117 |
//=========================================== |
|
109 | 118 |
|
119 |
// (6) Write a function that validates roman numerals according |
|
120 |
// to the rules given in the CW. |
|
121 |
||
122 |
def isValidNumeral(digitList: RomanNumeral): Boolean = |
|
123 |
||
124 |
||
125 |
||
126 |
// some test cases |
|
127 |
val invalids = List("IXC", "XCX", "IIII", "IIIII", "DD", "VL", |
|
128 |
"MIM", "XXCIII", "LXXIIX", "IIIIX", "IIXX", |
|
129 |
"ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX") |
|
130 |
||
131 |
val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI", |
|
132 |
"MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III", |
|
133 |
"XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC", |
|
134 |
"MDCLXVI") |
|
135 |
||
136 |
// (7) Write a recursive function that converts an Integer into a |
|
137 |
// a roman numeral. The input will be between 0 and 3999. |
|
138 |
||
139 |
def Int2Roman(n: Int): RomanNumeral = |
|
140 |
||
141 |
// (8) Write a function that reads a text file containing valid and |
|
142 |
// invalid roman numerals. Convert all valid roman numerals into integers, |
|
143 |
// add them up and produce the result as a roman numeral (using the |
|
144 |
// function in (7) |
|
145 |
||
146 |
def addvalidromanfile(filename: String) = |
|
147 |
||
148 |
// a test case |
|
149 |
//addvalidromanfile("roman2.txt") |