author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 25 Jan 2017 14:51:09 +0000 | |
changeset 106 | fae9db0d9793 |
parent 105 | 67ce930b5935 |
child 109 | 293ea84d82ca |
permissions | -rw-r--r-- |
6 | 1 |
\documentclass{article} |
62 | 2 |
\usepackage{../style} |
78 | 3 |
\usepackage{../langs} |
6 | 4 |
|
5 |
\begin{document} |
|
6 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
7 |
\section*{Replacement Coursework 1 (Roman Numerals)} |
6 | 8 |
|
100 | 9 |
This coursework is worth 10\%. It is about translating roman numerals |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
10 |
into integers and also about validating roman numerals. The coursework |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
11 |
is due on 2 February at 5pm. Make sure the files you submit can be |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
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
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
21 |
inside a class or object. Also note that the running time will be |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
22 |
restricted to a maximum of 360 seconds. |
86 | 23 |
|
6 | 24 |
|
100 | 25 |
\subsection*{Disclaimer} |
6 | 26 |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
27 |
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
|
28 |
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
|
29 |
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
|
30 |
you can freely use.\bigskip |
6 | 31 |
|
32 |
||
100 | 33 |
\subsection*{Part 1 (Translation)} |
6 | 34 |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
35 |
\noindent |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
36 |
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
|
37 |
$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
|
38 |
internal representation using the datatypes \texttt{RomanDigit} and |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
39 |
\texttt{RomanNumeral}, and then from this internal representation |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
40 |
converted into an Integer. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
41 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
42 |
\begin{itemize} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
43 |
\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
|
44 |
transforms a list of options into an option of a list. For example, |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
45 |
if you have the lists on the left, they should be transformed into |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
46 |
the option on the right: |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
47 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
48 |
\begin{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
49 |
\begin{tabular}{lcl} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
50 |
\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
|
51 |
\texttt{Some(List(1, 2, 3))} \\ |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
52 |
\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
|
53 |
\texttt{None} \\ |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
54 |
\texttt{List()} & $\Rightarrow$ & \texttt{Some(List())} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
55 |
\end{tabular} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
56 |
\end{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
57 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
a list of all \texttt{Some}s. In case the list is empty, it |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
61 |
produces \texttt{Some} of the empty list. \hfill[1 Mark] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
62 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
63 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
64 |
\item[(2)] Write a function first a function that converts a character |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
65 |
$I$, $V$, $X$, $L$, $C$, $D$, or $M$ into an option of a \texttt{RomanDigit}. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
66 |
If it is one of the roman digits, it should produce \texttt{Some}; |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
67 |
otherwise \texttt{None}. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
68 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
69 |
Next write a function that converts a string into a \texttt{RomanNumeral}. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
70 |
Again, this function should return an \texttt{Option}: |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
71 |
If the string consists of $I$, $V$, $X$, $L$, $C$, $D$, and $M$ only, then |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
72 |
it produces \texttt{Some}; otherwise if there is any other character in |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
73 |
the string, it should produce \texttt{None}. The empty string is just |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
74 |
the empty \texttt{RomanNumeral}, that is empty list of \texttt{RomanDigit}'s. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
75 |
You should use the function under Task (1) to produce the result. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
76 |
\hfill[2 Marks] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
77 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
78 |
\item[(3)] Write a recursive function RomanNumral2Int that converts a |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
79 |
\texttt{RomanNumeral} into an integer. You can assume the generated |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
80 |
integer will be between 0 and 3999. The argument of the function is |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
81 |
a list of roman digits. It should look how this list starts and then |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
82 |
calculate what the corresponding integer is for this ``start'' and |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
83 |
add it with the integer for the rest of the list. That means if the |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
84 |
argument is of the form shown on the left-hand side, it should do |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
85 |
the calculation on the right-hand side. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
86 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
87 |
\begin{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
88 |
\begin{tabular}{lcl} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
89 |
$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
|
90 |
$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
|
91 |
$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
|
92 |
$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
|
93 |
$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
|
94 |
$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
|
95 |
$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
|
96 |
$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
|
97 |
$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
|
98 |
$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
|
99 |
$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
|
100 |
$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
|
101 |
$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
|
102 |
\end{tabular} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
103 |
\end{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
104 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
105 |
The empty list will be converted into integer $0$.\hfill[1 Mark] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
106 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
107 |
\item[(4)] Write a function that takes a string and if possible |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
108 |
converts it into the internal representation. If successful, then |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
109 |
calculate the integer (an option of an integer) according to the |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
110 |
function in (3). If this is not possible, then return |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
111 |
\texttt{None}.\hfill[1 Mark] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
112 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
113 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
114 |
\item[(5)] The file \texttt{roman.txt} contains a list of roman numerals. |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
115 |
Read in these numerals, convert them into integers and then add them all |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
116 |
up. The function for reading a file is |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
117 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
118 |
\begin{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
119 |
\texttt{Source.fromFile("filename")("ISO-8859-9")} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
120 |
\end{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
121 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
122 |
Make sure you process the strings correctly by ignoring whitespaces |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
123 |
where neded.\\ \mbox{}\hfill[1 Mark] |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
124 |
\end{itemize} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
125 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
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: |