author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 21 Nov 2022 15:57:45 +0000 | |
changeset 447 | f51e593903ac |
parent 265 | 59779ce322a6 |
permissions | -rw-r--r-- |
265 | 1 |
// Replacement Exam: Scala Part |
2 |
//======================================= |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
3 |
|
265 | 4 |
// Roman Digits and Numerals |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
5 |
|
104
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
abstract class RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case object I extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case object V extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object X extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case object L extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object C extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case object D extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case object M extends RomanDigit |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
14 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
15 |
type RomanNumeral = List[RomanDigit] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
16 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
17 |
// (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
|
18 |
// 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
|
19 |
// 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
|
20 |
// 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
|
21 |
|
265 | 22 |
//def optionlist[A](xs: List[Option[A]]): Option[List[A]] = |
23 |
||
24 |
||
25 |
// some testcases |
|
26 |
//optionlist(List(Some(1), Some(2), Some(3))) // -> Some(List(1, 2, 3)) |
|
27 |
//optionlist(List(Some(1), None, Some(3))) // -> None |
|
28 |
//optionlist(List()) // -> Some(List()) |
|
29 |
||
30 |
||
31 |
||
105
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 |
// (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
|
34 |
// into a roman digit (if possible). Then convert a string into |
265 | 35 |
// a roman numeral (if possible). In both cases, if the conversion |
36 |
// is not possible, the functions should return None. |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
37 |
|
265 | 38 |
//def Char2RomanDigit(c: Char): Option[RomanDigit] = |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
39 |
|
265 | 40 |
//def String2RomanNumeral(s: String) : Option[RomanNumeral] = |
105
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 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
43 |
// some test cases |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
44 |
//String2RomanNumeral("IIII") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
45 |
//String2RomanNumeral("IV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
46 |
//String2RomanNumeral("VI") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
47 |
//String2RomanNumeral("IX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
48 |
//String2RomanNumeral("MCMLXXIX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
49 |
//String2RomanNumeral("MCMXLIV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
50 |
//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
|
51 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
52 |
// (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
|
53 |
// RomanNumeral into an integer. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
54 |
|
265 | 55 |
//def RomanNumeral2Int(rs: RomanNumeral): Int = |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
56 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
57 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
58 |
// some test cases |
265 | 59 |
//RomanNumeral2Int(List(I,I,I,I)) // 4 |
60 |
//RomanNumeral2Int(List(I,V)) // 4 |
|
61 |
//RomanNumeral2Int(List(V,I)) // 6 |
|
62 |
//RomanNumeral2Int(List(I,X)) // 9 |
|
63 |
//RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979 |
|
64 |
//RomanNumeral2Int(List(M,C,M,X,L,I,V)) // 1944 |
|
65 |
||
66 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
67 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
68 |
// (4) Write a function that converts a string (containing |
265 | 69 |
// a roman numeral) into an integer (if possible). If |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
70 |
// 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
|
71 |
|
265 | 72 |
//def String2Int(s: String): Option[Int] = |
109 | 73 |
|
74 |
||
75 |
||
76 |
// some test cases |
|
265 | 77 |
//String2Int("IIII") // 4 (though invalid roman numeral) |
78 |
//String2Int("IV") // 4 |
|
79 |
//String2Int("VI") // 6 |
|
80 |
//String2Int("IX") // 9 |
|
81 |
//String2Int("MCMLXXIX") // 1979 |
|
82 |
//String2Int("MCMXLIV") // 1944 |
|
83 |
//String2Int("") // 0 |
|
84 |
//String2Int("MDCLXI") // 1661 |
|
85 |
//String2Int("MMMCMXCIX") // 3999 |
|
86 |
//String2Int("XLVIII") // 48 |
|
87 |
//String2Int("MMVIII") // 2008 |
|
109 | 88 |
|
265 | 89 |
//String2Int("MMXI") // 2011 |
90 |
//String2Int("MIM") // 1999 |
|
91 |
//String2Int("MCMLVI") // 1956 |
|
109 | 92 |
|
265 | 93 |
//String2Int("III") // 3 |
94 |
//String2Int("XXX") // 30 |
|
95 |
//String2Int("CCC") // 300 |
|
96 |
//String2Int("MMM") // 3000 |
|
97 |
//String2Int("VII") // 7 |
|
98 |
//String2Int("LXVI") // 66 |
|
99 |
//String2Int("CL") // 150 |
|
100 |
//String2Int("MCC") // 1200 |
|
101 |
//String2Int("IV") // 4 |
|
102 |
//String2Int("IX") // 9 |
|
103 |
//String2Int("XC") // 90 |
|
104 |
//String2Int("MDCLXVI") // 1666 |
|
109 | 105 |
|
265 | 106 |
//String2Int("VIV") // 9 (but should be written as IX) |
107 |
//String2Int("IVX") // 14 (also invalid) |
|
109 | 108 |
|
265 | 109 |
// error cases |
110 |
//String2Int("I I I I") |
|
111 |
//String2Int("MC?I") |
|
112 |
//String2Int("abc") |
|
109 | 113 |
|
265 | 114 |