author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 09 Nov 2020 14:34:37 +0000 | |
changeset 357 | 826a8dbb658c |
parent 265 | 59779ce322a6 |
permissions | -rw-r--r-- |
108 | 1 |
// Part 1 about Roman Numerals |
2 |
//============================= |
|
3 |
||
104
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
abstract class RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case object I extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case object V extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case object X extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case object L extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object C extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case object D extends RomanDigit |
07780accd5df
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object M extends RomanDigit |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
12 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
13 |
type RomanNumeral = List[RomanDigit] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
14 |
|
108 | 15 |
|
16 |
// (1) First write a polymorphic function that recursively |
|
17 |
// transforms a list of options into an option of a list. |
|
18 |
// As soon as a None is inside the list, the result is None. |
|
19 |
// Otherwise produce a list of all Some's appended. |
|
20 |
||
105
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]] = xs match { |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
22 |
case Nil => Some(Nil) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
23 |
case x::xs => (x, optionlist(xs)) match { |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
24 |
case (None, _) => None |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
25 |
case (_, None) => None |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
26 |
case (Some(y), Some(ys)) => Some(y::ys) |
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 |
} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
29 |
|
108 | 30 |
optionlist(List(Some(1), Some(2), Some(3))) |
31 |
optionlist(List(Some(1), None, Some(3))) |
|
32 |
optionlist(List()) |
|
33 |
||
34 |
// (2) Write a function first a function that converts a character |
|
35 |
// into a roman digit (if possible). Then convert a string into |
|
36 |
// a roman numeral (if possible). If this is not possible, the functions |
|
37 |
// should return None. |
|
38 |
||
39 |
def Char2RomanDigit(c: Char): Option[RomanDigit] = c match { |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
40 |
case 'I' => Some(I) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
41 |
case 'V' => Some(V) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
42 |
case 'X' => Some(X) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
43 |
case 'L' => Some(L) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
44 |
case 'C' => Some(C) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
45 |
case 'D' => Some(D) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
46 |
case 'M' => Some(M) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
47 |
case _ => None |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
48 |
} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
49 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
50 |
def String2RomanNumeral(s: String) : Option[RomanNumeral] = |
108 | 51 |
optionlist(s.toList.map(Char2RomanDigit)) |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
52 |
|
108 | 53 |
// some test cases |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
54 |
String2RomanNumeral("IIII") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
55 |
String2RomanNumeral("IV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
56 |
String2RomanNumeral("VI") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
57 |
String2RomanNumeral("IX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
58 |
String2RomanNumeral("MCMLXXIX") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
59 |
String2RomanNumeral("MCMXLIV") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
60 |
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
|
61 |
|
108 | 62 |
|
63 |
// (3) Write a recursive function RomanNumral2Int that converts a |
|
64 |
// RomanNumeral into an integer. |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
65 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
66 |
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
67 |
case Nil => 0 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
68 |
case M::r => 1000 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
69 |
case C::M::r => 900 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
70 |
case D::r => 500 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
71 |
case C::D::r => 400 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
72 |
case C::r => 100 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
73 |
case X::C::r => 90 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
74 |
case L::r => 50 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
75 |
case X::L::r => 40 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
76 |
case X::r => 10 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
77 |
case I::X::r => 9 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
78 |
case V::r => 5 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
79 |
case I::V::r => 4 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
80 |
case I::r => 1 + RomanNumeral2Int(r) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
81 |
} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
82 |
|
108 | 83 |
// some test cases |
109 | 84 |
RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid roman number) |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
85 |
RomanNumeral2Int(List(I,V)) // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
86 |
RomanNumeral2Int(List(V,I)) // 6 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
87 |
RomanNumeral2Int(List(I,X)) // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
91 |
|
108 | 92 |
// (4) Write a function that converts a string (containing |
93 |
// a roman numeral) into an integer (if possible). If not |
|
94 |
// this is not possible, the functions should return None. |
|
95 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
96 |
def String2Int(s: String): Option[Int] = |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
97 |
String2RomanNumeral(s).map(RomanNumeral2Int(_)) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
98 |
|
109 | 99 |
// some test cases |
100 |
String2Int("IIII") // 4 (though invalid roman numeral) |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
101 |
String2Int("IV") // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
102 |
String2Int("VI") // 6 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
103 |
String2Int("IX") // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
104 |
String2Int("MCMLXXIX") // 1979 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
105 |
String2Int("MCMXLIV") // 1944 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
106 |
String2Int("") // 0 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
107 |
String2Int("MDCLXI") // 1661 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
108 |
String2Int("MMMCMXCIX") // 3999 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
109 |
String2Int("XLVIII") // 48 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
110 |
String2Int("MMVIII") // 2008 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
111 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
112 |
String2Int("MMXI") // 2011 |
111 | 113 |
String2Int("MIM") // 2001 |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
114 |
String2Int("MCMLVI") // 1956 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
115 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
116 |
String2Int("III") // 3 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
117 |
String2Int("XXX") // 30 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
118 |
String2Int("CCC") // 300 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
119 |
String2Int("MMM") // 3000 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
120 |
String2Int("VII") // 7 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
121 |
String2Int("LXVI") // 66 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
122 |
String2Int("CL") // 150 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
123 |
String2Int("MCC") // 1200 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
124 |
String2Int("IV") // 4 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
125 |
String2Int("IX") // 9 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
126 |
String2Int("XC") // 90 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
127 |
String2Int("MDCLXVI") // 1666 |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
128 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
129 |
|
265 | 130 |
// error/none cases |
131 |
String2Int("MC?I") |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
132 |
String2Int("abc") |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
133 |
|
109 | 134 |
|
108 | 135 |
|
109 | 136 |
// Part 2 about Validation of Roman Numerals |
137 |
//=========================================== |
|
108 | 138 |
|
109 | 139 |
def Digit2Int(r: RomanDigit) = r match { |
140 |
case I => 1 |
|
141 |
case V => 5 |
|
142 |
case X => 10 |
|
143 |
case L => 50 |
|
144 |
case C => 100 |
|
145 |
case D => 500 |
|
146 |
case M => 1000 |
|
147 |
} |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
148 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
149 |
def runsAllowed(r: RomanDigit) = r match { |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
150 |
case I | X | C | M => true |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
151 |
case V | L | D => false |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
152 |
} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
153 |
|
109 | 154 |
def subtractable(r1: RomanDigit, r2: RomanDigit) = (r1, r2) match { |
155 |
case (I, V) | (I, X) => true |
|
156 |
case (X, L) | (X, C) => true |
|
157 |
case (C, D) | (C, M) => true |
|
158 |
case _ => false |
|
159 |
} |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
160 |
|
109 | 161 |
def isValidNumeral(digitList: RomanNumeral): Boolean = digitList match { |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
162 |
|
109 | 163 |
// empty list is valid |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
164 |
case Nil => true |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
165 |
|
109 | 166 |
// no more than three runnables in succession |
167 |
case d1::d2::d3::d4::_ if (d1 == d2 && d1 == d3 && d1 == d4 && runsAllowed(d1)) => false |
|
168 |
||
169 |
// no more than one non-runnables in succession |
|
170 |
case d1::d2::_ if (d1 == d2 && !runsAllowed(d1)) => false |
|
171 |
||
172 |
// subtractable |
|
173 |
case d1::d2::_ if (Digit2Int(d1) < Digit2Int(d2) && !subtractable(d1, d2)) => false |
|
174 |
||
175 |
// followable1 |
|
176 |
case d1::d2::d3::_ if (Digit2Int(d2) < Digit2Int(d3) && subtractable(d2,d3) && |
|
177 |
Digit2Int(d1) < Digit2Int(d2) * 10) => false |
|
178 |
||
179 |
// followable2 |
|
180 |
case d1::d2::d3::_ if (Digit2Int(d1) < Digit2Int(d2) && subtractable(d1,d2) && |
|
181 |
Digit2Int(d1) <= Digit2Int(d3)) => false |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
182 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
183 |
// A single digit is always allowed |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
184 |
case _::ds => isValidNumeral(ds) |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
185 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
186 |
} |
109 | 187 |
|
188 |
||
189 |
val invalids = List("IXC", "XCX", "IIII", "IIIII", "DD", "VL", "MIM", "XXCIII", "LXXIIX", "IIIIX", |
|
190 |
"IIXX", "ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX") |
|
191 |
invalids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_)) |
|
192 |
invalids.map(String2RomanNumeral(_)).flatten.map((r) => (r, isValidNumeral(r))) |
|
193 |
||
194 |
||
195 |
val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI", |
|
196 |
"MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III", |
|
197 |
"XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC", |
|
198 |
"MDCLXVI") |
|
199 |
valids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_)) |
|
200 |
||
201 |
||
202 |
||
203 |
||
204 |
||
205 |
def Int2Roman(n: Int): RomanNumeral = n match { |
|
206 |
case 0 => Nil |
|
207 |
case n if (1000 <= n) => M :: Int2Roman(n - 1000) |
|
208 |
case n if (900 <= n) => C :: M :: Int2Roman(n - 900) |
|
209 |
case n if (500 <= n) => D :: Int2Roman(n - 500) |
|
210 |
case n if (400 <= n) => C :: D :: Int2Roman(n - 400) |
|
211 |
case n if (100 <= n) => C :: Int2Roman(n - 100) |
|
212 |
case n if (90 <= n) => X :: C :: Int2Roman(n - 90) |
|
213 |
case n if (50 <= n) => L :: Int2Roman(n - 50) |
|
214 |
case n if (40 <= n) => X :: L :: Int2Roman(n - 40) |
|
215 |
case n if (10 <= n) => X :: Int2Roman(n - 10) |
|
216 |
case n if (9 <= n) => I :: X :: Int2Roman(n - 9) |
|
217 |
case n if (5 <= n) => V :: Int2Roman(n - 5) |
|
218 |
case n if (4 <= n) => I :: V :: Int2Roman(n - 4) |
|
219 |
case n if (1 <= n) => I :: Int2Roman(n - 1) |
|
220 |
} |
|
221 |
||
222 |
||
223 |
Int2Roman(4) |
|
224 |
Int2Roman(6) |
|
225 |
Int2Roman(9) |
|
226 |
Int2Roman(1979) |
|
227 |
Int2Roman(1944) |
|
228 |
||
229 |
RomanNumeral2Int(List(I,V)) |
|
230 |
RomanNumeral2Int(List(V,I)) |
|
231 |
RomanNumeral2Int(List(I,X)) |
|
232 |
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) |
|
233 |
RomanNumeral2Int(List(M,C,M,X,L,I,V)) |
|
234 |
||
235 |