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