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