| author | Christian Urban <urbanc@in.tum.de> | 
| Thu, 26 Jan 2017 01:43:31 +0000 | |
| changeset 108 | 833e17ba391c | 
| parent 105 | 0f9f774c7697 | 
| child 109 | 6b362ee692c3 | 
| permissions | -rw-r--r-- | 
| 6 | 1 | \documentclass{article}
 | 
| 62 | 2 | \usepackage{../style}
 | 
| 78 | 3 | \usepackage{../langs}
 | 
| 6 | 4 | |
| 5 | \begin{document}
 | |
| 6 | ||
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 7 | \section*{Replacement Coursework 1 (Roman Numerals)}
 | 
| 6 | 8 | |
| 100 | 9 | This coursework is worth 10\%. It is about translating roman numerals | 
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 10 | into integers and also about validating roman numerals. The coursework | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 11 | is due on 2 February at 5pm. Make sure the files you submit can be | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 12 | processed by just calling \texttt{scala <<filename.scala>>}.\bigskip
 | 
| 62 | 13 | |
| 14 | \noindent | |
| 15 | \textbf{Important:} Do not use any mutable data structures in your
 | |
| 74 | 16 | submission! They are not needed. This excludes the use of | 
| 62 | 17 | \texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
 | 
| 68 | 18 | code! It has a different meaning in Scala, than in Java. Do not use | 
| 19 | \texttt{var}! This declares a mutable variable.  Make sure the
 | |
| 62 | 20 | functions you submit are defined on the ``top-level'' of Scala, not | 
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 21 | inside a class or object. Also note that the running time will be | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 22 | restricted to a maximum of 360 seconds. | 
| 86 | 23 | |
| 6 | 24 | |
| 100 | 25 | \subsection*{Disclaimer}
 | 
| 6 | 26 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 27 | It should be understood that the work you submit represents your own | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 28 | effort! You have not copied from anyone else. An exception is the | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 29 | Scala code I showed during the lectures or uploaded to KEATS, which | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 30 | you can freely use.\bigskip | 
| 6 | 31 | |
| 32 | ||
| 100 | 33 | \subsection*{Part 1 (Translation)}
 | 
| 6 | 34 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 35 | \noindent | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 36 | Roman numerals are strings consisting of the letters $I$, $V$, $X$, | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 37 | $L$, $C$, $D$, and $M$. Such strings should be transformed into an | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 38 | internal representation using the datatypes \texttt{RomanDigit} and
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 39 | \texttt{RomanNumeral}, and then from this internal representation
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 40 | converted into an Integer. | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 41 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 42 | \begin{itemize}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 43 | \item[(1)] First write a polymorphic function that recursively | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 44 | transforms a list of options into an option of a list. For example, | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 45 | if you have the lists on the left, they should be transformed into | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 46 | the option on the right: | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 47 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 48 |   \begin{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 49 |   \begin{tabular}{lcl}  
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 50 |     \texttt{List(Some(1), Some(2), Some(3))} & $\Rightarrow$ &
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 51 |     \texttt{Some(List(1, 2, 3))} \\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 52 |     \texttt{List(Some(1), None, Some(3))} & $\Rightarrow$ &
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 53 |     \texttt{None} \\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 54 |     \texttt{List()} & $\Rightarrow$ & \texttt{Some(List())}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 55 |   \end{tabular}  
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 56 |   \end{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 57 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 58 |   This means the function should produce \texttt{None} as soon
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 59 |   as a \texttt{None} is inside the list. Otherwise it produces
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 60 |   a list of all \texttt{Some}s. In case the list is empty, it
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 61 |   produces \texttt{Some} of the empty list. \hfill[1 Mark]
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 62 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 63 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 64 | \item[(2)] Write a function first a function that converts a character | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 65 |   $I$, $V$, $X$, $L$, $C$, $D$, or $M$ into an option of a \texttt{RomanDigit}.
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 66 |   If it is one of the roman digits, it should produce \texttt{Some};
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 67 |   otherwise \texttt{None}.
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 68 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 69 |   Next write a function that converts a string into a \texttt{RomanNumeral}.
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 70 |   Again, this function should return an \texttt{Option}:
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 71 | If the string consists of $I$, $V$, $X$, $L$, $C$, $D$, and $M$ only, then | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 72 |   it produces \texttt{Some}; otherwise if there is any other character in
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 73 |   the string, it should produce \texttt{None}. The empty string is just
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 74 |   the empty \texttt{RomanNumeral}, that is empty list of \texttt{RomanDigit}'s.
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 75 | You should use the function under Task (1) to produce the result. | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 76 | \hfill[2 Marks] | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 77 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 78 | \item[(3)] Write a recursive function RomanNumral2Int that converts a | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 79 |   \texttt{RomanNumeral} into an integer. You can assume the generated
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 80 | integer will be between 0 and 3999. The argument of the function is | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 81 | a list of roman digits. It should look how this list starts and then | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 82 | calculate what the corresponding integer is for this ``start'' and | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 83 | add it with the integer for the rest of the list. That means if the | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 84 | argument is of the form shown on the left-hand side, it should do | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 85 | the calculation on the right-hand side. | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 86 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 87 |   \begin{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 88 |   \begin{tabular}{lcl}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 89 |     $M::r$    & $\Rightarrow$ & $1000 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 90 |     $C::M::r$ & $\Rightarrow$ & $900 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 91 |     $D::r$    & $\Rightarrow$ & $500 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 92 |     $C::D::r$ & $\Rightarrow$ & $400 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 93 |     $C::r$    & $\Rightarrow$ & $100 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 94 |     $X::C::r$ & $\Rightarrow$ & $90 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 95 |     $L::r$    & $\Rightarrow$ & $50 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 96 |     $X::L::r$ & $\Rightarrow$ & $40 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 97 |     $X::r$    & $\Rightarrow$ & $10 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 98 |     $I::X::r$ & $\Rightarrow$ & $9 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 99 |     $V::r$    & $\Rightarrow$ & $5 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 100 |     $I::V::r$ & $\Rightarrow$ & $4 + \text{roman numeral of rest}\; r$\\
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 101 |     $I::r$    & $\Rightarrow$ & $1 + \text{roman numeral of rest}\; r$
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 102 |   \end{tabular}  
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 103 |   \end{center}    
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 104 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 105 | The empty list will be converted into integer $0$.\hfill[1 Mark] | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 106 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 107 | \item[(4)] Write a function that takes a string and if possible | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 108 | converts it into the internal representation. If successful, then | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 109 | calculate the integer (an option of an integer) according to the | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 110 | function in (3). If this is not possible, then return | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 111 |   \texttt{None}.\hfill[1 Mark]
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 112 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 113 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 114 | \item[(5)] The file \texttt{roman.txt} contains a list of roman numerals.
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 115 | Read in these numerals, convert them into integers and then add them all | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 116 | up. The function for reading a file is | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 117 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 118 |   \begin{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 119 |   \texttt{Source.fromFile("filename")("ISO-8859-9")}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 120 |   \end{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 121 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 122 | Make sure you process the strings correctly by ignoring whitespaces | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 123 |   where neded.\\ \mbox{}\hfill[1 Mark]
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 124 | \end{itemize}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 125 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 126 | |
| 100 | 127 | \subsection*{Part 2 (Validation)}
 | 
| 78 | 128 | |
| 6 | 129 | |
| 130 | \end{document}
 | |
| 131 | ||
| 68 | 132 | |
| 6 | 133 | %%% Local Variables: | 
| 134 | %%% mode: latex | |
| 135 | %%% TeX-master: t | |
| 136 | %%% End: |