cws/cw07.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 01:03:09 +0000
changeset 401 9471c3b7ea02
parent 265 59779ce322a6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     2
\usepackage{../style}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
     3
\usepackage{../langs}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     7
\section*{Scala Part (Roman Numerals)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     9
This coursework is worth 50\%. It is about translating roman numerals
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    10
into integers.  Make sure the files you submit can be
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    11
processed by just calling
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    12
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    13
\begin{center}
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    14
  \texttt{scala <<filename.scala>>}
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    15
\end{center}%\bigskip
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    16
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    17
\noindent
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    18
\textbf{Important:} Do not use any mutable data structures in your
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    19
submission! They are not needed. This menas you cannot use 
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    20
\texttt{ListBuffer}s, \texttt{Array}s, for example. Do not use \texttt{return} in your
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    21
code! It has a different meaning in Scala, than in Java.  Do not use
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    22
\texttt{var}! This declares a mutable variable.  Make sure the
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    23
functions you submit are defined on the ``top-level'' of Scala, not
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    24
inside a class or object. 
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    25
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
100
93260be6770f updated
Christian Urban <urbanc@in.tum.de>
parents: 94
diff changeset
    27
\subsection*{Disclaimer}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    29
It should be understood that the work you submit represents your own
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    30
effort! You have not copied from anyone else. An exception is the
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    31
Scala code I showed during the lectures or uploaded to KEATS, which
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    32
you can freely use.\bigskip
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    35
\subsection*{Tasks}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    37
\noindent
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    38
Roman numerals are strings consisting of the letters $I$, $V$, $X$,
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    39
$L$, $C$, $D$, and $M$. Such strings should be transformed into an
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    40
internal representation using the datatypes \texttt{RomanDigit} and
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    41
\texttt{RomanNumeral} (defined in \texttt{roman.scala}), and then from
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    42
this internal representation converted into Integers.
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    43
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    44
\begin{itemize}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    45
\item[(1)] First write a polymorphic function that recursively
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    46
  transforms a list of options into an option of a list. For example,
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    47
  if you have the lists on the left-hand side below, they should be transformed into
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    48
  the options on the right-hand side:
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    49
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    50
  \begin{center}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    51
  \begin{tabular}{lcl}  
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    52
    \texttt{List(Some(1), Some(2), Some(3))} & $\Rightarrow$ &
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    53
    \texttt{Some(List(1, 2, 3))} \\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    54
    \texttt{List(Some(1), None, Some(3))} & $\Rightarrow$ &
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    55
    \texttt{None} \\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    56
    \texttt{List()} & $\Rightarrow$ & \texttt{Some(List())}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    57
  \end{tabular}  
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    58
  \end{center}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    59
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    60
  This means the function should produce \texttt{None} as soon
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    61
  as a \texttt{None} is inside the list. Otherwise it produces
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    62
  a list of all \texttt{Some}s. In case the list is empty, it
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    63
  produces \texttt{Some} of the empty list. \hfill[15\% Marks]
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    64
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    65
 
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    66
\item[(2)] Write first a function that converts the characters $I$, $V$,
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    67
  $X$, $L$, $C$, $D$, and $M$ into an option of a \texttt{RomanDigit}.
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    68
  If the input is one of the roman digits, the function should produce \texttt{Some};
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    69
  otherwise \texttt{None}.
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    70
  
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    71
  Next write a function that converts a string into a
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    72
  \texttt{RomanNumeral}.  Again, this function should return an
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    73
  \texttt{Option}: If the string consists of $I$, $V$, $X$, $L$, $C$,
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    74
  $D$, and $M$ only, then it produces \texttt{Some}; otherwise if
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    75
  there is any other character in the string, it should produce
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    76
  \texttt{None}. The empty string is just the empty
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    77
  \texttt{RomanNumeral}, that is the empty list of
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    78
  \texttt{RomanDigit}'s.  You should use the function under Task (1)
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    79
  to produce the result.  \hfill[15\% Marks]
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    80
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    81
\item[(3)] Write a recursive function \texttt{RomanNumral2Int} that
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    82
  converts a \texttt{RomanNumeral} into an integer. You can assume the
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    83
  generated integer will be between 0 and 3999.  The argument of the
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    84
  function is a list of roman digits. It should analyse how this list
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    85
  starts and then calculate what the corresponding integer is for this
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    86
  ``start'' and add it with the integer for the rest of the list. That
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    87
  means if the argument is of the form shown on the left-hand side, it
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
    88
  should do the calculation on the right-hand side.
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    89
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    90
  \begin{center}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    91
  \begin{tabular}{lcl}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    92
    $M::r$    & $\Rightarrow$ & $1000 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    93
    $C::M::r$ & $\Rightarrow$ & $900 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    94
    $D::r$    & $\Rightarrow$ & $500 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    95
    $C::D::r$ & $\Rightarrow$ & $400 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    96
    $C::r$    & $\Rightarrow$ & $100 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    97
    $X::C::r$ & $\Rightarrow$ & $90 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    98
    $L::r$    & $\Rightarrow$ & $50 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    99
    $X::L::r$ & $\Rightarrow$ & $40 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   100
    $X::r$    & $\Rightarrow$ & $10 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   101
    $I::X::r$ & $\Rightarrow$ & $9 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   102
    $V::r$    & $\Rightarrow$ & $5 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   103
    $I::V::r$ & $\Rightarrow$ & $4 + \text{roman numeral of rest}\; r$\\
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   104
    $I::r$    & $\Rightarrow$ & $1 + \text{roman numeral of rest}\; r$
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   105
  \end{tabular}  
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   106
  \end{center}    
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   107
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   108
  The empty list will be converted to integer $0$.\hfill[10\% Mark]
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   109
  
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   110
\item[(4)] Write a function that takes a string as input and if possible
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
  converts it into the internal representation of Roman Numerals. If successful, it then
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
  calculates the corresponding integer (actually an option of an integer) according to the
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   113
  function in (3).  If this is not possible, then return
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   114
  \texttt{None}.\\
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   115
  \mbox{}\hfill[10\% Mark]
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   116
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   117
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   118
%\item[(5)] The file \texttt{roman.txt} contains a list of roman numerals.
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   119
%  Read in these numerals, convert them into integers and then add them all
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   120
%  up. The Scala function for reading a file is
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   121
%
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   122
%  \begin{center}
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   123
%  \texttt{Source.fromFile("filename")("ISO-8859-9")}
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   124
%  \end{center}
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   125
%
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   126
%  Make sure you process the strings correctly by ignoring whitespaces
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   127
%  where needed.\\ \mbox{}\hfill[1 Mark]
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   128
\end{itemize}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   129
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   130
\end{document}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   131
100
93260be6770f updated
Christian Urban <urbanc@in.tum.de>
parents: 94
diff changeset
   132
\subsection*{Part 2 (Validation)}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   133
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   134
As you can see the function under Task (3) can produce some unexpected
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   135
results. For example for $XXCIII$ it produces 103. The reason for this
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   136
unexpected result is that $XXCIII$ is actually not a valid roman
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   137
number, neither is $IIII$ for 4 nor $MIM$ for 1999. Although actual
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   138
Romans were not so fussy about this,\footnote{They happily used
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   139
  numbers like $XIIX$ or $IIXX$ for 18.} but modern times declared
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   140
that there are precise rules for what a valid roman number is, namely:
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   141
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   142
\begin{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   143
\item Repeatable roman digits are $I$, $X$, $C$ and $M$. The other ones
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   144
  are non-repeatable. Repeatable digits can be repeated upto 3 times in a
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   145
  number (for example $MMM$ is OK); non-repeatable digits cannot be
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   146
  repeated at all (for example $VV$ is excluded).
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   147
  
111
cd6b9fe4bce5 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   148
\item If a smaller digits precedes a bigger digit, then $I$ can precede $V$ and $X$; $X$ can preced
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   149
  $L$ and $C$; and $C$ can preced $D$ and $M$. No other combination is permitted in this case.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   150
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   151
\item If a smaller digit precedes a bigger digit (for example $IV$), then the smaller number   
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   152
  must be either the first digit in the number, or follow a digit which is at least 10 times its value.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   153
  So $VIV$ is excluded, because $I$ follows $V$ and $I * 10$ is bigger than $V$; but $XIV$ is
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   154
  allowed, because $I$ follows $X$ and $I * 10$ is equal to $X$.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   155
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   156
\item Let us say two digits are called a \emph{compound} roman digit
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   157
  when a smaller digit precedes a bigger digit (so $IV$, $XL$, $CM$
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   158
  for example). If a compound digit is followed by another digit, then
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   159
  this digit must be smaller than the first digit in the compound
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   160
  digit. For example $IXI$ is excluded, but $XLI$ is not.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   161
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   162
\item The empty roman numeral is valid.  
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   163
\end{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   164
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   165
\noindent
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   166
The tasks in this part are as follows:
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   167
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   168
\begin{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   169
\item[(6)] Implement a recursive function \texttt{isValidNumeral} that
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   170
  takes a \texttt{RomanNumeral} as argument and produces true if \textbf{all}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   171
  the rules above are satisfied, and otherwise false.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   172
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   173
  Hint: It might be more convenient to test when the rules fail and then return false;
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   174
  return true in all other cases.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   175
  \mbox{}\hfill[2 Marks]
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   176
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   177
\item[(7)] Write a recursive function that converts an Integer into a \texttt{RomanNumeral}.
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   178
  You can assume the function will only be called for integers between 0 and 3999.\mbox{}\hfill[1 Mark]
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   179
  
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   180
\item[(8)] Write a function that reads a text file (for example \texttt{roman2.txt})
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   181
  containing valid and invalid roman numerals. Convert all valid roman numerals into
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   182
  integers, add them up and produce the result as a \texttt{RomanNumeral} (using the function
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   183
  from (7)). \hfill[1 Mark]
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   184
\end{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   185
  
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   189
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
%%% End: