\documentclass{article}
\usepackage{../style}
\usepackage{../langs}
\begin{document}
\section*{Replacement Coursework 1 (Roman Numerals)}
This coursework is worth 10\%. It is about translating roman numerals
into integers and also about validating roman numerals. The coursework
is due on 2 February at 5pm. Make sure the files you submit can be
processed by just calling \texttt{scala <<filename.scala>>}.\bigskip
\noindent
\textbf{Important:} Do not use any mutable data structures in your
submission! They are not needed. This excludes the use of
\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
code! It has a different meaning in Scala, than in Java. Do not use
\texttt{var}! This declares a mutable variable. Make sure the
functions you submit are defined on the ``top-level'' of Scala, not
inside a class or object. Also note that the running time will be
restricted to a maximum of 360 seconds.
\subsection*{Disclaimer}
It should be understood that the work you submit represents your own
effort! You have not copied from anyone else. An exception is the
Scala code I showed during the lectures or uploaded to KEATS, which
you can freely use.\bigskip
\subsection*{Part 1 (Translation)}
\noindent
Roman numerals are strings consisting of the letters $I$, $V$, $X$,
$L$, $C$, $D$, and $M$. Such strings should be transformed into an
internal representation using the datatypes \texttt{RomanDigit} and
\texttt{RomanNumeral}, and then from this internal representation
converted into an Integer.
\begin{itemize}
\item[(1)] First write a polymorphic function that recursively
transforms a list of options into an option of a list. For example,
if you have the lists on the left, they should be transformed into
the option on the right:
\begin{center}
\begin{tabular}{lcl}
\texttt{List(Some(1), Some(2), Some(3))} & $\Rightarrow$ &
\texttt{Some(List(1, 2, 3))} \\
\texttt{List(Some(1), None, Some(3))} & $\Rightarrow$ &
\texttt{None} \\
\texttt{List()} & $\Rightarrow$ & \texttt{Some(List())}
\end{tabular}
\end{center}
This means the function should produce \texttt{None} as soon
as a \texttt{None} is inside the list. Otherwise it produces
a list of all \texttt{Some}s. In case the list is empty, it
produces \texttt{Some} of the empty list. \hfill[1 Mark]
\item[(2)] Write a function first a function that converts a character
$I$, $V$, $X$, $L$, $C$, $D$, or $M$ into an option of a \texttt{RomanDigit}.
If it is one of the roman digits, it should produce \texttt{Some};
otherwise \texttt{None}.
Next write a function that converts a string into a \texttt{RomanNumeral}.
Again, this function should return an \texttt{Option}:
If the string consists of $I$, $V$, $X$, $L$, $C$, $D$, and $M$ only, then
it produces \texttt{Some}; otherwise if there is any other character in
the string, it should produce \texttt{None}. The empty string is just
the empty \texttt{RomanNumeral}, that is empty list of \texttt{RomanDigit}'s.
You should use the function under Task (1) to produce the result.
\hfill[2 Marks]
\item[(3)] Write a recursive function RomanNumral2Int that converts a
\texttt{RomanNumeral} into an integer. You can assume the generated
integer will be between 0 and 3999. The argument of the function is
a list of roman digits. It should look how this list starts and then
calculate what the corresponding integer is for this ``start'' and
add it with the integer for the rest of the list. That means if the
argument is of the form shown on the left-hand side, it should do
the calculation on the right-hand side.
\begin{center}
\begin{tabular}{lcl}
$M::r$ & $\Rightarrow$ & $1000 + \text{roman numeral of rest}\; r$\\
$C::M::r$ & $\Rightarrow$ & $900 + \text{roman numeral of rest}\; r$\\
$D::r$ & $\Rightarrow$ & $500 + \text{roman numeral of rest}\; r$\\
$C::D::r$ & $\Rightarrow$ & $400 + \text{roman numeral of rest}\; r$\\
$C::r$ & $\Rightarrow$ & $100 + \text{roman numeral of rest}\; r$\\
$X::C::r$ & $\Rightarrow$ & $90 + \text{roman numeral of rest}\; r$\\
$L::r$ & $\Rightarrow$ & $50 + \text{roman numeral of rest}\; r$\\
$X::L::r$ & $\Rightarrow$ & $40 + \text{roman numeral of rest}\; r$\\
$X::r$ & $\Rightarrow$ & $10 + \text{roman numeral of rest}\; r$\\
$I::X::r$ & $\Rightarrow$ & $9 + \text{roman numeral of rest}\; r$\\
$V::r$ & $\Rightarrow$ & $5 + \text{roman numeral of rest}\; r$\\
$I::V::r$ & $\Rightarrow$ & $4 + \text{roman numeral of rest}\; r$\\
$I::r$ & $\Rightarrow$ & $1 + \text{roman numeral of rest}\; r$
\end{tabular}
\end{center}
The empty list will be converted into integer $0$.\hfill[1 Mark]
\item[(4)] Write a function that takes a string and if possible
converts it into the internal representation. If successful, then
calculate the integer (an option of an integer) according to the
function in (3). If this is not possible, then return
\texttt{None}.\hfill[1 Mark]
\item[(5)] The file \texttt{roman.txt} contains a list of roman numerals.
Read in these numerals, convert them into integers and then add them all
up. The function for reading a file is
\begin{center}
\texttt{Source.fromFile("filename")("ISO-8859-9")}
\end{center}
Make sure you process the strings correctly by ignoring whitespaces
where neded.\\ \mbox{}\hfill[1 Mark]
\end{itemize}
\subsection*{Part 2 (Validation)}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: