11 \mbox{}\hfill\textit{coupled with judiciously placed print statements.''}\smallskip\\ |
11 \mbox{}\hfill\textit{coupled with judiciously placed print statements.''}\smallskip\\ |
12 \mbox{}\hfill\textit{ --- Brian W. Kernighan, in Unix for Beginners (1979)}\bigskip |
12 \mbox{}\hfill\textit{ --- Brian W. Kernighan, in Unix for Beginners (1979)}\bigskip |
13 |
13 |
14 |
14 |
15 \noindent |
15 \noindent |
16 This assignemnt is about Scala and worth 10\%. The first and second |
16 This assignment is about Scala and worth 10\%. The first and second |
17 part are due on 16 November at 11pm, and the third part on 21 December |
17 part are due on 16 November at 11pm, and the third part on 21 December |
18 at 11pm. You are asked to implement two programs about list |
18 at 11pm. You are asked to implement two programs about list |
19 processing and recursion. The third part is more advanced and might |
19 processing and recursion. The third part is more advanced and might |
20 include material you have not yet seen in the first lecture. |
20 include material you have not yet seen in the first lecture. |
21 \bigskip |
21 \bigskip |
26 Also note that the running time of each part will be restricted to a |
26 Also note that the running time of each part will be restricted to a |
27 maximum of 30 seconds on my laptop. |
27 maximum of 30 seconds on my laptop. |
28 |
28 |
29 \DISCLAIMER{} |
29 \DISCLAIMER{} |
30 |
30 |
31 \subsubsection*{Reference Implementation} |
31 \subsection*{Reference Implementation} |
32 |
32 |
33 Like the C++ assignments, the Scala assignments will work like this: you |
33 Like the C++ assignments, the Scala assignments will work like this: you |
34 push your files to GitHub and receive (after sometimes a long delay) some |
34 push your files to GitHub and receive (after sometimes a long delay) some |
35 automated feedback. In the end we take a snapshot of the submitted files and |
35 automated feedback. In the end we take a snapshot of the submitted files and |
36 apply an automated marking script to them. |
36 apply an automated marking script to them. |
53 ... |
53 ... |
54 scala> CW6a.collatz_max(6) |
54 scala> CW6a.collatz_max(6) |
55 ... |
55 ... |
56 \end{lstlisting}%$ |
56 \end{lstlisting}%$ |
57 |
57 |
|
58 \subsection*{Hints} |
|
59 |
|
60 \noindent |
|
61 \textbf{For Part 1:} useful math operators: \texttt{\%} for modulo; useful |
|
62 functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt}, |
|
63 \texttt{.toList} for conversions, \texttt{List(...).max} for the |
|
64 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of |
|
65 a value in a list.\bigskip |
|
66 |
|
67 \noindent |
|
68 \textbf{For Part 2 + 3:} useful string functions: \texttt{.startsWith(...)} for |
|
69 checking whether a string has a given prefix, \texttt{\_ ++ \_} for |
|
70 concatenating two strings; useful option functions: \texttt{.flatten} |
|
71 flattens a list of options such that it filters way all |
|
72 \texttt{None}'s, \texttt{Try(...).getOrElse ...} runs some code that |
|
73 might raise an exception---if yes, then a default value can be given; |
|
74 useful list functions: \texttt{.head} for obtaining the first element |
|
75 in a non-empty list, \texttt{.length} for the length of a |
|
76 list; \texttt{.filter(...)} for filtering out elements in a list; \texttt{.getLines.toList} for obtaining a list of lines from |
|
77 a file; \texttt{.split(",").toList} for splitting strings according to |
|
78 a comma.\bigskip |
|
79 |
|
80 \noindent |
|
81 Fortunately Scala supports operator overloading. But make sure you understand the difference between \texttt{100 / 3} and |
|
82 \texttt{100.0 / 3}! |
|
83 |
|
84 \newpage |
58 \subsection*{Part 1 (3 Marks, file collatz.scala)} |
85 \subsection*{Part 1 (3 Marks, file collatz.scala)} |
59 |
86 |
60 This part is about recursion. You are asked to implement a Scala |
87 This part is about recursion. You are asked to implement a Scala |
61 program that tests examples of the \emph{$3n + 1$-conjecture}, also |
88 program that tests examples of the \emph{$3n + 1$-conjecture}, also |
62 called \emph{Collatz conjecture}. This conjecture can be described as |
89 called \emph{Collatz conjecture}. This conjecture can be described as |
132 \item 1 to 1 Million where $837,799$ takes 524 steps |
159 \item 1 to 1 Million where $837,799$ takes 524 steps |
133 %% runs out of stack space |
160 %% runs out of stack space |
134 %% \item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 685 steps |
161 %% \item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 685 steps |
135 \end{itemize} |
162 \end{itemize} |
136 |
163 |
137 \noindent |
164 |
138 \textbf{Hints:} useful math operators: \texttt{\%} for modulo; useful |
|
139 functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt}, |
|
140 \texttt{.toList} for conversions, \texttt{List(...).max} for the |
|
141 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of |
|
142 a value in a list. |
|
143 |
165 |
144 |
166 |
145 |
167 |
146 \subsection*{Part 2 (3 Marks, file drumb.scala)} |
168 \subsection*{Part 2 (3 Marks, file drumb.scala)} |
147 |
169 |
172 \item We do this for 40 years until January 2018 and check what would |
194 \item We do this for 40 years until January 2018 and check what would |
173 have become out of our \$100. |
195 have become out of our \$100. |
174 \end{itemize} |
196 \end{itemize} |
175 |
197 |
176 \noindent |
198 \noindent |
177 Until Yahoo was bought by Altaba this summer, historical stock market |
199 Until Yahoo was bought by Altaba last year, historical stock market |
178 data for such back-of-the-envelope calculations was freely available |
200 data for such back-of-the-envelope calculations was freely available |
179 online. Unfortuantely nowadays this kind of data is difficult to |
201 online. Unfortunately nowadays this kind of data is more difficult to |
180 obtain, unless you are prepared to pay extortionate prices or be |
202 obtain, unless you are prepared to pay extortionate prices or be |
181 severely rate-limited. Therefore this coursework comes with a number |
203 severely rate-limited. Therefore this assignment comes with a number |
182 of files containing CSV-lists with the historical stock prices for the |
204 of files containing CSV-lists with the historical stock prices for the |
183 companies in our portfolios. Use these files for the following |
205 companies in our portfolios. Use these files for the following |
184 tasks.\bigskip |
206 tasks.\bigskip |
185 |
207 |
|
208 \newpage |
186 \noindent |
209 \noindent |
187 \textbf{Tasks} |
210 \textbf{Tasks} |
188 |
211 |
189 \begin{itemize} |
212 \begin{itemize} |
190 \item[(1)] Write a function \texttt{get\_january\_data} that takes a |
213 \item[(1)] Write a function \texttt{get\_january\_data} that takes a |
220 |
243 |
221 \begin{verbatim} |
244 \begin{verbatim} |
222 List(List(Some(311.349976), Some(20.544939)), |
245 List(List(Some(311.349976), Some(20.544939)), |
223 List(Some(300.222351), Some(31.638695)), |
246 List(Some(300.222351), Some(31.638695)), |
224 List(Some(330.555054), Some(39.478039))) |
247 List(Some(330.555054), Some(39.478039))) |
225 \end{verbatim}\hfill[1 Marks] |
248 \end{verbatim}\hfill[1 Mark] |
226 \end{itemize} |
249 \end{itemize} |
227 |
250 |
228 \subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)} |
251 \subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)} |
229 |
252 |
230 \noindent |
253 \noindent |
295 balance of \$100 into roughly \$101,589 for real estate and a whopping |
318 balance of \$100 into roughly \$101,589 for real estate and a whopping |
296 \$1,587,528 for blue chips. Note when comparing these results with your |
319 \$1,587,528 for blue chips. Note when comparing these results with your |
297 own calculations: there might be some small rounding errors, which |
320 own calculations: there might be some small rounding errors, which |
298 when compounded lead to moderately different values.\bigskip |
321 when compounded lead to moderately different values.\bigskip |
299 |
322 |
300 \noindent |
|
301 \textbf{Hints:} useful string functions: \texttt{.startsWith(...)} for |
|
302 checking whether a string has a given prefix, \texttt{\_ ++ \_} for |
|
303 concatenating two strings; useful option functions: \texttt{.flatten} |
|
304 flattens a list of options such that it filters way all |
|
305 \texttt{None}'s, \texttt{Try(...) getOrElse ...} runs some code that |
|
306 might raise an exception---if yes, then a default value can be given; |
|
307 useful list functions: \texttt{.head} for obtaining the first element |
|
308 in a non-empty list, \texttt{.length} for the length of a |
|
309 list.\bigskip |
|
310 |
|
311 |
323 |
312 \noindent |
324 \noindent |
313 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
325 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
314 our yield in many ways: first, who can know in 1978 about what will |
326 our yield in many ways: first, who can know in 1978 about what will |
315 turn out to be a blue chip company. Also, since the portfolios are |
327 turn out to be a blue chip company. Also, since the portfolios are |
346 the corresponding CSV-list. This list from the URL above should |
358 the corresponding CSV-list. This list from the URL above should |
347 contain 194 lines.\medskip |
359 contain 194 lines.\medskip |
348 |
360 |
349 \noindent |
361 \noindent |
350 Write another function that can read the file \texttt{population.csv} |
362 Write another function that can read the file \texttt{population.csv} |
351 from disk (the file is distributed with the coursework). This |
363 from disk (the file is distributed with the assignment). This |
352 function should take a string as argument, the file name, and again |
364 function should take a string as argument, the file name, and again |
353 return a list of strings corresponding to each entry in the |
365 return a list of strings corresponding to each entry in the |
354 CSV-list. For \texttt{population.csv}, this list should contain 216 |
366 CSV-list. For \texttt{population.csv}, this list should contain 216 |
355 lines.\hfill[1 Mark] |
367 lines.\hfill[1 Mark] |
356 |
368 |