diff -r d39b8733c6ea -r 293ea84d82ca cws/cw04.tex --- a/cws/cw04.tex Thu Jan 26 01:43:31 2017 +0000 +++ b/cws/cw04.tex Fri Jan 27 14:55:56 2017 +0000 @@ -36,14 +36,14 @@ 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. +\texttt{RomanNumeral} (defined in \texttt{roman.scala}), and then from +this internal representation converted into Integers. \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: + if you have the lists on the left-hand side, they should be transformed into + the option on the right-hand side: \begin{center} \begin{tabular}{lcl} @@ -61,28 +61,29 @@ 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}. +\item[(2)] Write first a function that converts the characters $I$, $V$, + $X$, $L$, $C$, $D$, and $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] + 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 the 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. +\item[(3)] Write a recursive function \texttt{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} @@ -102,30 +103,82 @@ \end{tabular} \end{center} - The empty list will be converted into integer $0$.\hfill[1 Mark] + The empty list will be converted to 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 + converts it into the internal representation. If successful, it then + calculates 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 + up. The Scala 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] + where needed.\\ \mbox{}\hfill[1 Mark] \end{itemize} \subsection*{Part 2 (Validation)} +As you can see the function under Task (3) can produce some unexpected +results. For example for $XXCIII$ it produces 103. The reason for this +unexpected result is that $XXCIII$ is actually not a valid roman +number, neither is $IIII$ for 4 nor $MIM$ for 1999. Although actual +Romans were not so fussy about this,\footnote{They happily used + numbers like $XIIX$ or $IIXX$ for 18.} but modern times declared +that there are precise rules for what a valid roman number is, namely: + +\begin{itemize} +\item Repeatable roman digits are $I$, $X$, $C$ and $M$. The other ones + are non-repeatable. Repeatable digits can be repeated upto 3 times in a + number (for example $MMM$ is OK); non-repeatable digits cannot be + repeated at all (for example $VV$ is excluded). + +\item If a smaller digits precedes a bigger digit, then $I$ can precede $V$ and $C$; $X$ can preced + $L$ and $C$; and $C$ can preced $D$ and $M$. No other combination is permitted in this case. + +\item If a smaller digit precedes a bigger digit (for example $IV$), then the smaller number + must be either the first digit in the number, or follow a digit which is at least 10 times its value. + So $VIV$ is excluded, because $I$ follows $V$ and $I * 10$ is bigger than $V$; but $XIV$ is + allowed, because $I$ follows $X$ and $I * 10$ is equal to $X$. + +\item Let us say two digits are called a \emph{compound} roman digit + when a smaller digit precedes a bigger digit (so $IV$, $XL$, $CM$ + for example). If a compound digit is followed by another digit, then + this digit must be smaller than the first digit in the compound + digit. For example $IXI$ is excluded, but $XLI$ is not. + +\item The empty roman numeral is valid. +\end{itemize} + +\noindent +The tasks in this part are as follows: + +\begin{itemize} +\item[(6)] Implement a recursive function \texttt{isValidNumeral} that + takes a \texttt{RomanNumeral} as argument and produces true if \textbf{all} + the rules above are satisfied, and otherwise false. + + Hint: It might be more convenient to test when the rules fail and then return false; + return true in all other cases. + \mbox{}\hfill[2 Marks] + +\item[(7)] Write a recursive function that converts an Integer into a \texttt{RomanNumeral}. + You can assume the function will only be called for integers between 0 and 3999.\mbox{}\hfill[1 Mark] + +\item[(8)] Write a function that reads a text file (for example \texttt{roman2.txt}) + containing valid and invalid roman numerals. Convert all valid roman numerals into + integers, add them up and produce the result as a \texttt{RomanNumeral} (using the function + from (7)). \hfill[1 Mark] +\end{itemize} + \end{document}