progs/roman.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 19 Nov 2019 06:38:20 +0000
changeset 322 755d165633ec
parent 265 59779ce322a6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
     1
// Replacement Exam: Scala Part
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
     2
//=======================================
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
     3
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
     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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    22
//def optionlist[A](xs: List[Option[A]]): Option[List[A]] = 
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    23
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    24
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    25
// some testcases
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    26
//optionlist(List(Some(1), Some(2), Some(3)))  // -> Some(List(1, 2, 3))
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    27
//optionlist(List(Some(1), None, Some(3)))     // -> None
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    28
//optionlist(List())                           // -> Some(List())
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    29
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    30
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    35
// a roman numeral (if possible). In both  cases, if the conversion 
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    59
//RomanNumeral2Int(List(I,I,I,I))         // 4
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    60
//RomanNumeral2Int(List(I,V))             // 4
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    61
//RomanNumeral2Int(List(V,I))             // 6
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    62
//RomanNumeral2Int(List(I,X))             // 9
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    63
//RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    64
//RomanNumeral2Int(List(M,C,M,X,L,I,V))   // 1944
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    65
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    72
//def String2Int(s: String): Option[Int] = 
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    73
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    74
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    75
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    76
// some test cases
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    77
//String2Int("IIII")      // 4 (though invalid roman numeral)  
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    78
//String2Int("IV")        // 4
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    79
//String2Int("VI")        // 6
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    80
//String2Int("IX")        // 9
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    81
//String2Int("MCMLXXIX")  // 1979
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    82
//String2Int("MCMXLIV")   // 1944
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    83
//String2Int("")          // 0
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    84
//String2Int("MDCLXI")    // 1661
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    85
//String2Int("MMMCMXCIX") // 3999
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    86
//String2Int("XLVIII")    // 48
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    87
//String2Int("MMVIII")    // 2008
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    88
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    89
//String2Int("MMXI")      // 2011 
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    90
//String2Int("MIM")       // 1999
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    91
//String2Int("MCMLVI")    // 1956 
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    92
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    93
//String2Int("III") 	// 3
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    94
//String2Int("XXX") 	// 30
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    95
//String2Int("CCC") 	// 300
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    96
//String2Int("MMM") 	// 3000
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    97
//String2Int("VII") 	// 7
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    98
//String2Int("LXVI") 	// 66
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    99
//String2Int("CL") 	// 150
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   100
//String2Int("MCC") 	// 1200
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   101
//String2Int("IV") 	// 4
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   102
//String2Int("IX") 	// 9
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   103
//String2Int("XC") 	// 90
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   104
//String2Int("MDCLXVI")	// 1666
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   105
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   106
//String2Int("VIV")       // 9 (but should be written as IX) 
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   107
//String2Int("IVX")       // 14 (also invalid)
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   108
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   109
// error cases
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   110
//String2Int("I I I I")
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   111
//String2Int("MC?I")
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   112
//String2Int("abc")
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   113
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   114