progs/roman.scala
changeset 105 67ce930b5935
parent 104 07780accd5df
child 108 d39b8733c6ea
equal deleted inserted replaced
104:07780accd5df 105:67ce930b5935
       
     1 // Part 1 about Roman Numerals
       
     2 //=============================
       
     3 
       
     4 
     1 abstract class RomanDigit 
     5 abstract class RomanDigit 
     2 case object I extends RomanDigit 
     6 case object I extends RomanDigit 
     3 case object V extends RomanDigit 
     7 case object V extends RomanDigit 
     4 case object X extends RomanDigit 
     8 case object X extends RomanDigit 
     5 case object L extends RomanDigit 
     9 case object L extends RomanDigit 
     6 case object C extends RomanDigit 
    10 case object C extends RomanDigit 
     7 case object D extends RomanDigit 
    11 case object D extends RomanDigit 
     8 case object M extends RomanDigit 
    12 case object M extends RomanDigit 
       
    13 
       
    14 type RomanNumeral = List[RomanDigit] 
       
    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 
       
    21 def optionlist[A](xs: List[Option[A]]): Option[List[A]] = 
       
    22 
       
    23 // (2) Write a function first a function that converts a character
       
    24 // into a roman digit (if possible). Then convert a string into
       
    25 // a roman numeral (if possible). If this is not possible, the functions
       
    26 // should return None.
       
    27 
       
    28 def Char2RomanDigit(c: Char): Option[RomanDigit] = 
       
    29 
       
    30 def String2RomanNumeral(s: String) : Option[RomanNumeral] = 
       
    31 
       
    32 
       
    33 // some test cases
       
    34 //String2RomanNumeral("IIII")
       
    35 //String2RomanNumeral("IV")
       
    36 //String2RomanNumeral("VI")
       
    37 //String2RomanNumeral("IX")
       
    38 //String2RomanNumeral("MCMLXXIX")
       
    39 //String2RomanNumeral("MCMXLIV") 
       
    40 //String2RomanNumeral("M C M X L I V")  // None
       
    41 
       
    42 // (3) Write a recursive function RomanNumral2Int that converts a
       
    43 // RomanNumeral into an integer.
       
    44 
       
    45 def RomanNumeral2Int(rs: RomanNumeral): Int = 
       
    46 
       
    47 
       
    48 // some test cases
       
    49 RomanNumeral2Int(List(I,I,I,I))         // 4
       
    50 RomanNumeral2Int(List(I,V))             // 4
       
    51 RomanNumeral2Int(List(V,I))             // 6
       
    52 RomanNumeral2Int(List(I,X))             // 9
       
    53 RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
       
    54 RomanNumeral2Int(List(M,C,M,X,L,I,V))   // 1944
       
    55 
       
    56 // (4) Write a function that converts a string (containing
       
    57 // a roman numeral) into an integer (if possible). If not
       
    58 // this is not possible, the functions should return None.
       
    59 
       
    60 def String2Int(s: String): Option[Int] = 
       
    61 
       
    62 // some test cases
       
    63 String2Int("IIII")      // 4 (though invalid roman numeral)  
       
    64 String2Int("IV")        // 4
       
    65 String2Int("VI")        // 6
       
    66 String2Int("IX")        // 9
       
    67 String2Int("MCMLXXIX")  // 1979
       
    68 String2Int("MCMXLIV")   // 1944
       
    69 String2Int("")          // 0
       
    70 String2Int("MDCLXI")    // 1661
       
    71 String2Int("MMMCMXCIX") // 3999
       
    72 String2Int("XLVIII")    // 48
       
    73 String2Int("MMVIII")    // 2008
       
    74 
       
    75 String2Int("MMXI")      // 2011 
       
    76 String2Int("MIM")       // 1999
       
    77 String2Int("MCMLVI")    // 1956 
       
    78 
       
    79 String2Int("III") 	// 3
       
    80 String2Int("XXX") 	// 30
       
    81 String2Int("CCC") 	// 300
       
    82 String2Int("MMM") 	// 3000
       
    83 String2Int("VII") 	// 7
       
    84 String2Int("LXVI") 	// 66
       
    85 String2Int("CL") 	// 150
       
    86 String2Int("MCC") 	// 1200
       
    87 String2Int("IV") 	// 4
       
    88 String2Int("IX") 	// 9
       
    89 String2Int("XC") 	// 90
       
    90 String2Int("MDCLXVI")	// 1666
       
    91 
       
    92 String2Int("VIV")       // 9 (but should be written as IX) 
       
    93 String2Int("IVX")       // 14 (also invalid)
       
    94 
       
    95 // error cases
       
    96 String2Int("MC?I")
       
    97 String2Int("abc")
       
    98 
       
    99 
       
   100 
       
   101 
       
   102 // (5) The file roman.txt contains a list of roman numerals. 
       
   103 // Read in these numerals, convert them into integers and then 
       
   104 // add them all up.
       
   105 
       
   106 import io.Source
       
   107 import scala.util._
       
   108 
       
   109 // function for reading files:
       
   110 // Source.fromFile("file_name")("ISO-8859-9")