progs/roman_sol.scala
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--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
     1
// Part 1 about Roman Numerals
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
     2
//=============================
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
     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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    15
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    16
// (1) First write a polymorphic function that recursively
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    17
// transforms a list of options into an option of a list.
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    18
// As soon as a None is inside the list, the result is None. 
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    19
// Otherwise produce a list of all Some's appended.
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    30
optionlist(List(Some(1), Some(2), Some(3)))
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    31
optionlist(List(Some(1), None, Some(3)))
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    32
optionlist(List())
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    33
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    34
// (2) Write a function first a function that converts a character
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    35
// into a roman digit (if possible). Then convert a string into
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    36
// a roman numeral (if possible). If this is not possible, the functions
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    37
// should return None.
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    38
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    62
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    63
// (3) Write a recursive function RomanNumral2Int that converts a
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    83
// some test cases
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    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
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    92
// (4) Write a function that converts a string (containing
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    93
// a roman numeral) into an integer (if possible). If not
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    94
// this is not possible, the functions should return None.
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
    99
// some test cases
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   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
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   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
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   130
// error/none cases
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   134
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   135
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   136
// Part 2 about Validation of Roman Numerals
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   137
//===========================================
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   138
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   139
def Digit2Int(r: RomanDigit) = r match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   140
  case I => 1
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   141
  case V => 5
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   142
  case X => 10
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   143
  case L => 50
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   144
  case C => 100
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   145
  case D => 500
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   146
  case M => 1000
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   154
def subtractable(r1: RomanDigit, r2: RomanDigit) = (r1, r2)  match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   155
  case (I, V) | (I, X) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   156
  case (X, L) | (X, C) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   157
  case (C, D) | (C, M) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   158
  case _ => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   159
}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   160
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   166
  // no more than three runnables in succession
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   167
  case d1::d2::d3::d4::_ if (d1 == d2 && d1 == d3 && d1 == d4 && runsAllowed(d1)) => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   168
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   169
  // no more than one non-runnables in succession
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   170
  case d1::d2::_ if (d1 == d2 && !runsAllowed(d1)) => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   171
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   172
  // subtractable
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   173
  case d1::d2::_ if (Digit2Int(d1) < Digit2Int(d2) && !subtractable(d1, d2)) => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   174
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   175
  // followable1
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   176
  case d1::d2::d3::_ if (Digit2Int(d2) < Digit2Int(d3) && subtractable(d2,d3) && 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   177
                         Digit2Int(d1) < Digit2Int(d2) * 10) => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   178
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   179
  // followable2 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   180
  case d1::d2::d3::_ if (Digit2Int(d1) < Digit2Int(d2) && subtractable(d1,d2) && 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   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
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   187
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   188
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   189
val invalids = List("IXC", "XCX", "IIII", "IIIII", "DD", "VL", "MIM", "XXCIII", "LXXIIX", "IIIIX",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   190
                    "IIXX", "ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX")
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   191
invalids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   192
invalids.map(String2RomanNumeral(_)).flatten.map((r) => (r, isValidNumeral(r)))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   193
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   194
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   195
val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   196
                  "MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   197
                  "XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   198
                  "MDCLXVI")
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   199
valids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   200
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   201
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   202
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   203
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   204
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   205
def Int2Roman(n: Int): RomanNumeral = n match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   206
  case 0 => Nil
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   207
  case n if (1000 <= n) => M :: Int2Roman(n - 1000)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   208
  case n if (900 <= n) => C :: M :: Int2Roman(n - 900)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   209
  case n if (500 <= n) => D :: Int2Roman(n - 500)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   210
  case n if (400 <= n) => C :: D :: Int2Roman(n - 400)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   211
  case n if (100 <= n) => C :: Int2Roman(n - 100)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   212
  case n if (90 <= n) => X :: C :: Int2Roman(n - 90)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   213
  case n if (50 <= n) => L :: Int2Roman(n - 50)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   214
  case n if (40 <= n) => X :: L :: Int2Roman(n - 40)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   215
  case n if (10 <= n) => X :: Int2Roman(n - 10)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   216
  case n if (9 <= n) => I :: X :: Int2Roman(n - 9)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   217
  case n if (5 <= n) => V :: Int2Roman(n - 5)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   218
  case n if (4 <= n) => I :: V :: Int2Roman(n - 4)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   219
  case n if (1 <= n) => I :: Int2Roman(n - 1)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   220
}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   221
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   222
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   223
Int2Roman(4)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   224
Int2Roman(6)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   225
Int2Roman(9)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   226
Int2Roman(1979)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   227
Int2Roman(1944)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   228
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   229
RomanNumeral2Int(List(I,V))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   230
RomanNumeral2Int(List(V,I))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   231
RomanNumeral2Int(List(I,X))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   232
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   233
RomanNumeral2Int(List(M,C,M,X,L,I,V))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   234
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   235