265
|
1 |
// Replacement Exam: Scala Part
|
|
2 |
//=======================================
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
3 |
|
265
|
4 |
// Roman Digits and Numerals
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
5 |
|
104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
abstract class RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case object I extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case object V extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object X extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case object L extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object C extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case object D extends RomanDigit
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case object M extends RomanDigit
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
14 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
15 |
type RomanNumeral = List[RomanDigit]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
16 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
17 |
// (1) First write a polymorphic function that recursively
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
18 |
// transforms a list of options into an option of a list.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
19 |
// As soon as a None is inside the list, the result is None.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
20 |
// Otherwise produce a list of all Some's appended.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
32 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
33 |
// (2) Write a function first a function that converts a character
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
37 |
|
265
|
38 |
//def Char2RomanDigit(c: Char): Option[RomanDigit] =
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
39 |
|
265
|
40 |
//def String2RomanNumeral(s: String) : Option[RomanNumeral] =
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
41 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
42 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
43 |
// some test cases
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
44 |
//String2RomanNumeral("IIII")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
45 |
//String2RomanNumeral("IV")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
46 |
//String2RomanNumeral("VI")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
47 |
//String2RomanNumeral("IX")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
48 |
//String2RomanNumeral("MCMLXXIX")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
49 |
//String2RomanNumeral("MCMXLIV")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
50 |
//String2RomanNumeral("M C M X L I V") // None
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
51 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
52 |
// (3) Write a recursive function RomanNumral2Int that converts a
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
53 |
// RomanNumeral into an integer.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
54 |
|
265
|
55 |
//def RomanNumeral2Int(rs: RomanNumeral): Int =
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
56 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
57 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
67 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
68 |
// (4) Write a function that converts a string (containing
|
265
|
69 |
// a roman numeral) into an integer (if possible). If
|
105
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
70 |
// this is not possible, the functions should return None.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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 |
|