equal
deleted
inserted
replaced
124 String2Int("IV") // 4 |
124 String2Int("IV") // 4 |
125 String2Int("IX") // 9 |
125 String2Int("IX") // 9 |
126 String2Int("XC") // 90 |
126 String2Int("XC") // 90 |
127 String2Int("MDCLXVI") // 1666 |
127 String2Int("MDCLXVI") // 1666 |
128 |
128 |
129 String2Int("VIV") // 9 (but should be written as IX) |
129 |
130 String2Int("IVX") // 14 (also invalid) |
130 // error/none cases |
131 |
131 String2Int("MC?I") |
132 // error cases |
|
133 String2Int("MC?I") |
|
134 String2Int("abc") |
132 String2Int("abc") |
135 |
133 |
136 |
134 |
137 // (5) The file roman.txt contains a list of roman numerals. |
|
138 // Read in these numerals, convert them into integers and then |
|
139 // add them all up. |
|
140 |
|
141 import io.Source |
|
142 import scala.util._ |
|
143 |
|
144 // function for reading files: |
|
145 // Source.fromFile("file_name")("ISO-8859-9") |
|
146 |
|
147 def addromanfile(filename: String) = { |
|
148 val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim) |
|
149 lines.map(String2Int(_)).flatten.sum |
|
150 } |
|
151 |
|
152 |
|
153 addromanfile("roman.txt") |
|
154 |
|
155 val ls = """IIII |
|
156 IV |
|
157 VI |
|
158 IX |
|
159 MCMLXXIX |
|
160 MCMXLIV |
|
161 |
|
162 MDCLXI |
|
163 MMMCMXCIX |
|
164 XLVIII |
|
165 MMVIII |
|
166 |
|
167 MMXI |
|
168 MIM |
|
169 MCMLVI |
|
170 |
|
171 III |
|
172 XXX |
|
173 CCC |
|
174 MMM |
|
175 VII |
|
176 LXVI |
|
177 CL |
|
178 MCC |
|
179 IV |
|
180 IX |
|
181 XC |
|
182 MDCLXVI""".split("\n").map(_.trim).map(String2Int(_)).flatten |
|
183 |
|
184 String2Int("MIM") |
|
185 |
135 |
186 // Part 2 about Validation of Roman Numerals |
136 // Part 2 about Validation of Roman Numerals |
187 //=========================================== |
137 //=========================================== |
188 |
138 |
189 def Digit2Int(r: RomanDigit) = r match { |
139 def Digit2Int(r: RomanDigit) = r match { |
281 RomanNumeral2Int(List(I,X)) |
231 RomanNumeral2Int(List(I,X)) |
282 RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) |
232 RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) |
283 RomanNumeral2Int(List(M,C,M,X,L,I,V)) |
233 RomanNumeral2Int(List(M,C,M,X,L,I,V)) |
284 |
234 |
285 |
235 |
286 def addvalidromanfile(filename: String) = { |
|
287 val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim) |
|
288 val ints = lines.map(String2RomanNumeral(_)).flatten.filter(isValidNumeral(_)).map(RomanNumeral2Int(_)) |
|
289 ints.sum |
|
290 } |
|
291 |
|
292 |
|
293 addvalidromanfile("roman2.txt") |
|