progs/roman_sol.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 02 Feb 2019 13:35:07 +0000
changeset 262 21c69dc3dbf7
parent 111 cd6b9fe4bce5
child 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
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   129
String2Int("VIV")       // 9 (but should be written as IX) 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   130
String2Int("IVX")       // 14 (also invalid)
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   131
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   132
// error cases
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   133
String2Int("MC?I")
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   134
String2Int("abc")
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   135
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   136
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   137
// (5) The file roman.txt contains a list of roman numerals. 
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   138
// Read in these numerals, convert them into integers and then 
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   139
// add them all up.
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   140
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   141
import io.Source
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   142
import scala.util._
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   143
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   144
// function for reading files:
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   145
// Source.fromFile("file_name")("ISO-8859-9")
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   146
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   147
def addromanfile(filename: String) = {
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   148
  val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim)
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   149
  lines.map(String2Int(_)).flatten.sum
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   150
}
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   151
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   152
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   153
addromanfile("roman.txt")
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   154
111
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   155
val ls = """IIII
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   156
IV        
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   157
VI        
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   158
IX        
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   159
MCMLXXIX  
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   160
MCMXLIV   
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   161
          
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   162
MDCLXI    
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   163
MMMCMXCIX 
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   164
XLVIII    
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   165
MMVIII    
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   166
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   167
MMXI      
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   168
MIM       
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   169
MCMLVI    
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   170
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   171
III     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   172
XXX     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   173
CCC     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   174
MMM     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   175
VII     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   176
LXVI    
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   177
CL      
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   178
MCC     
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   179
IV      
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   180
IX      
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   181
XC      
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   182
MDCLXVI""".split("\n").map(_.trim).map(String2Int(_)).flatten
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   183
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
   184
String2Int("MIM")
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   185
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   186
// Part 2 about Validation of Roman Numerals
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   187
//===========================================
108
d39b8733c6ea updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   188
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   189
def Digit2Int(r: RomanDigit) = r match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   190
  case I => 1
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   191
  case V => 5
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   192
  case X => 10
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   193
  case L => 50
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   194
  case C => 100
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   195
  case D => 500
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   196
  case M => 1000
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   197
}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   198
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   199
def runsAllowed(r: RomanDigit) = r match {
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   200
  case I | X | C | M => true
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   201
  case V | L | D => false
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   202
}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   203
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   204
def subtractable(r1: RomanDigit, r2: RomanDigit) = (r1, r2)  match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   205
  case (I, V) | (I, X) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   206
  case (X, L) | (X, C) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   207
  case (C, D) | (C, M) => true
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   208
  case _ => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   209
}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   210
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   211
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
   212
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   213
  // empty list is valid
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   214
  case Nil => true
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   215
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   216
  // no more than three runnables in succession
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   217
  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
   218
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   219
  // no more than one non-runnables in succession
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   220
  case d1::d2::_ if (d1 == d2 && !runsAllowed(d1)) => false
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
  // subtractable
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   223
  case d1::d2::_ if (Digit2Int(d1) < Digit2Int(d2) && !subtractable(d1, d2)) => false
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   224
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   225
  // followable1
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   226
  case d1::d2::d3::_ if (Digit2Int(d2) < Digit2Int(d3) && subtractable(d2,d3) && 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   227
                         Digit2Int(d1) < Digit2Int(d2) * 10) => false
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
  // followable2 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   230
  case d1::d2::d3::_ if (Digit2Int(d1) < Digit2Int(d2) && subtractable(d1,d2) && 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   231
                         Digit2Int(d1) <= Digit2Int(d3))  => false 
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   232
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   233
    // A single digit is always allowed
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   234
  case _::ds => isValidNumeral(ds) 
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   235
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
   236
}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   237
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   238
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   239
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
   240
                    "IIXX", "ICM", "CIM", "VIV", "IVX", "MCMC", "XIIX", "IIXX")
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   241
invalids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   242
invalids.map(String2RomanNumeral(_)).flatten.map((r) => (r, isValidNumeral(r)))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   243
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   244
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   245
val valids = List("IV", "VI", "IX", "MCMLXXIX", "MCMXLIV", "", "MDCLXI",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   246
                  "MMMCMXCIX", "XLVIII", "MMVIII", "MMXI", "MCMLVI", "III",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   247
                  "XXX", "CCC", "MMM", "VII", "LXVI", "CL", "MCC", "XC",
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   248
                  "MDCLXVI")
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   249
valids.map(String2RomanNumeral(_)).flatten.map(isValidNumeral(_))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   250
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   251
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   252
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   253
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   254
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   255
def Int2Roman(n: Int): RomanNumeral = n match {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   256
  case 0 => Nil
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   257
  case n if (1000 <= n) => M :: Int2Roman(n - 1000)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   258
  case n if (900 <= n) => C :: M :: Int2Roman(n - 900)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   259
  case n if (500 <= n) => D :: Int2Roman(n - 500)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   260
  case n if (400 <= n) => C :: D :: Int2Roman(n - 400)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   261
  case n if (100 <= n) => C :: Int2Roman(n - 100)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   262
  case n if (90 <= n) => X :: C :: Int2Roman(n - 90)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   263
  case n if (50 <= n) => L :: Int2Roman(n - 50)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   264
  case n if (40 <= n) => X :: L :: Int2Roman(n - 40)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   265
  case n if (10 <= n) => X :: Int2Roman(n - 10)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   266
  case n if (9 <= n) => I :: X :: Int2Roman(n - 9)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   267
  case n if (5 <= n) => V :: Int2Roman(n - 5)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   268
  case n if (4 <= n) => I :: V :: Int2Roman(n - 4)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   269
  case n if (1 <= n) => I :: Int2Roman(n - 1)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   270
}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   271
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   272
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   273
Int2Roman(4)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   274
Int2Roman(6)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   275
Int2Roman(9)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   276
Int2Roman(1979)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   277
Int2Roman(1944)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   278
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   279
RomanNumeral2Int(List(I,V))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   280
RomanNumeral2Int(List(V,I))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   281
RomanNumeral2Int(List(I,X))             
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   282
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) 
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   283
RomanNumeral2Int(List(M,C,M,X,L,I,V))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   284
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   285
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   286
def addvalidromanfile(filename: String) = {
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   287
  val lines = Source.fromFile(filename)("ISO-8859-9").getLines.toList.map(_.trim)
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   288
  val ints = lines.map(String2RomanNumeral(_)).flatten.filter(isValidNumeral(_)).map(RomanNumeral2Int(_))
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   289
  ints.sum
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   290
}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   291
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   292
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 108
diff changeset
   293
addvalidromanfile("roman2.txt")