| author | Christian Urban <urbanc@in.tum.de> | 
| Fri, 07 Dec 2018 03:15:46 +0000 | |
| changeset 239 | e05032a1c102 | 
| parent 210 | 34f935e13bdd | 
| child 251 | a0bd77103898 | 
| permissions | -rw-r--r-- | 
| 6 | 1  | 
\documentclass{article}
 | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
2  | 
\usepackage{../style}
 | 
| 195 | 3  | 
\usepackage{disclaimer}
 | 
| 199 | 4  | 
\usepackage{../langs}
 | 
| 6 | 5  | 
|
6  | 
\begin{document}
 | 
|
7  | 
||
| 199 | 8  | 
\section*{Assignment 6 (Scala)}
 | 
9  | 
||
10  | 
\mbox{}\hfill\textit{``The most effective debugging tool is still careful thought,}\\
 | 
|
11  | 
\mbox{}\hfill\textit{coupled with judiciously placed print statements.''}\smallskip\\
 | 
|
12  | 
\mbox{}\hfill\textit{ --- Brian W. Kernighan, in Unix for Beginners (1979)}\bigskip
 | 
|
13  | 
||
14  | 
||
15  | 
\noindent  | 
|
| 201 | 16  | 
This assignment is about Scala and worth 10\%. The first and second  | 
| 202 | 17  | 
part are due on 15 November at 11pm, and the third part on 21 December  | 
| 199 | 18  | 
at 11pm. You are asked to implement two programs about list  | 
| 18 | 19  | 
processing and recursion. The third part is more advanced and might  | 
| 196 | 20  | 
include material you have not yet seen in the first lecture.  | 
21  | 
\bigskip  | 
|
| 199 | 22  | 
|
| 195 | 23  | 
\IMPORTANT{}
 | 
24  | 
||
| 127 | 25  | 
\noindent  | 
| 195 | 26  | 
Also note that the running time of each part will be restricted to a  | 
| 199 | 27  | 
maximum of 30 seconds on my laptop.  | 
| 192 | 28  | 
|
| 196 | 29  | 
\DISCLAIMER{}
 | 
| 6 | 30  | 
|
| 201 | 31  | 
\subsection*{Reference Implementation}
 | 
| 6 | 32  | 
|
| 199 | 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  | 
|
35  | 
automated feedback. In the end we take a snapshot of the submitted files and  | 
|
36  | 
apply an automated marking script to them.  | 
|
37  | 
||
38  | 
In addition, the Scala assignments come with a reference implementation  | 
|
39  | 
in form of a \texttt{jar}-file. This allows you to run any test cases
 | 
|
40  | 
on your own computer. For example you can call Scala on the command  | 
|
41  | 
line with the option \texttt{-cp collatz.jar} and then query any
 | 
|
42  | 
function from the template file. Say you want to find out what  | 
|
43  | 
the functions \texttt{collatz} and \texttt{collatz\_max}
 | 
|
44  | 
produce: for this you just need to prefix them with the object name  | 
|
45  | 
\texttt{CW6a} (and \texttt{CW6b} respectively for \texttt{drumb.jar}).
 | 
|
46  | 
If you want to find out what these functions produce for the argument  | 
|
47  | 
\texttt{6}, you would type something like:
 | 
|
48  | 
||
49  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
|
50  | 
$ scala -cp collatz.jar  | 
|
51  | 
||
52  | 
scala> CW6a.collatz(6)  | 
|
53  | 
...  | 
|
54  | 
scala> CW6a.collatz_max(6)  | 
|
55  | 
...  | 
|
56  | 
\end{lstlisting}%$
 | 
|
57  | 
||
| 201 | 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  | 
|
| 199 | 85  | 
\subsection*{Part 1 (3 Marks, file collatz.scala)}
 | 
| 6 | 86  | 
|
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
87  | 
This part is about recursion. You are asked to implement a Scala  | 
| 18 | 88  | 
program that tests examples of the \emph{$3n + 1$-conjecture}, also
 | 
89  | 
called \emph{Collatz conjecture}. This conjecture can be described as
 | 
|
90  | 
follows: Start with any positive number $n$ greater than $0$:  | 
|
91  | 
||
92  | 
\begin{itemize}
 | 
|
93  | 
\item If $n$ is even, divide it by $2$ to obtain $n / 2$.  | 
|
94  | 
\item If $n$ is odd, multiply it by $3$ and add $1$ to obtain $3n +  | 
|
95  | 
1$.  | 
|
96  | 
\item Repeat this process and you will always end up with $1$.  | 
|
97  | 
\end{itemize}
 | 
|
98  | 
||
99  | 
\noindent  | 
|
| 199 | 100  | 
For example if you start with $6$, or $9$, you obtain the  | 
| 18 | 101  | 
series  | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
102  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
103  | 
\[  | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
104  | 
\begin{array}{@{}l@{\hspace{5mm}}l@{}}
 | 
| 199 | 105  | 
6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 8 steps)}\\
 | 
106  | 
9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1  & \text{(= 19 steps)}\\
 | 
|
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
107  | 
\end{array}
 | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
108  | 
\]  | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
109  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
110  | 
\noindent  | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
111  | 
As you can see, the numbers go up and down like a roller-coaster, but  | 
| 18 | 112  | 
curiously they seem to always terminate in $1$. The conjecture is that  | 
113  | 
this will \emph{always} happen for every number greater than
 | 
|
114  | 
0.\footnote{While it is relatively easy to test this conjecture with
 | 
|
115  | 
particular numbers, it is an interesting open problem to  | 
|
| 196 | 116  | 
  \emph{prove} that the conjecture is true for \emph{all} numbers ($>
 | 
117  | 
  0$). Paul Erd\"o{}s, a famous mathematician you might have hard
 | 
|
118  | 
about, said about this conjecture: ``Mathematics may not be ready  | 
|
119  | 
for such problems.'' and also offered a \$500 cash prize for its  | 
|
120  | 
solution. Jeffrey Lagarias, another mathematician, claimed that  | 
|
121  | 
based only on known information about this problem, ``this is an  | 
|
122  | 
extraordinarily difficult problem, completely out of reach of  | 
|
123  | 
present day mathematics.'' There is also a  | 
|
124  | 
  \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
 | 
|
| 18 | 125  | 
  (click \href{https://xkcd.com/710/}{here}). If you are able to solve
 | 
126  | 
this conjecture, you will definitely get famous.}\bigskip  | 
|
127  | 
||
128  | 
\noindent  | 
|
| 199 | 129  | 
\textbf{Tasks}
 | 
| 18 | 130  | 
|
131  | 
\begin{itemize}
 | 
|
132  | 
\item[(1)] You are asked to implement a recursive function that  | 
|
133  | 
calculates the number of steps needed until a series ends  | 
|
| 199 | 134  | 
with $1$. In case of starting with $6$, it takes $8$ steps and in  | 
135  | 
case of starting with $9$, it takes $19$ (see above). In order to  | 
|
| 18 | 136  | 
try out this function with large numbers, you should use  | 
137  | 
  \texttt{Long} as argument type, instead of \texttt{Int}.  You can
 | 
|
138  | 
assume this function will be called with numbers between $1$ and  | 
|
| 196 | 139  | 
$1$ Million. \hfill[2 Marks]  | 
| 18 | 140  | 
|
141  | 
\item[(2)] Write a second function that takes an upper bound as  | 
|
142  | 
argument and calculates the steps for all numbers in the range from  | 
|
| 210 | 143  | 
1 up to this bound (the bound including). It returns the maximum number of  | 
144  | 
steps and the corresponding number that needs that many steps. More  | 
|
145  | 
precisely it returns a pair where the first component is the number  | 
|
146  | 
  of steps and the second is the corresponding number. \hfill\mbox{[1
 | 
|
147  | 
Mark]}  | 
|
| 18 | 148  | 
\end{itemize}
 | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
149  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
150  | 
\noindent  | 
| 18 | 151  | 
\textbf{Test Data:} Some test ranges are:
 | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
152  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
153  | 
\begin{itemize}
 | 
| 199 | 154  | 
\item 1 to 10 where $9$ takes 19 steps  | 
155  | 
\item 1 to 100 where $97$ takes 118 steps,  | 
|
156  | 
\item 1 to 1,000 where $871$ takes 178 steps,  | 
|
157  | 
\item 1 to 10,000 where $6,171$ takes 261 steps,  | 
|
158  | 
\item 1 to 100,000 where $77,031$ takes 350 steps,  | 
|
159  | 
\item 1 to 1 Million where $837,799$ takes 524 steps  | 
|
160  | 
%% runs out of stack space  | 
|
161  | 
%% \item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 685 steps  | 
|
| 196 | 162  | 
\end{itemize}
 | 
| 18 | 163  | 
|
| 201 | 164  | 
|
| 127 | 165  | 
|
166  | 
||
| 196 | 167  | 
|
| 199 | 168  | 
\subsection*{Part 2 (3 Marks, file drumb.scala)}
 | 
169  | 
||
170  | 
A purely fictional character named Mr T.~Drumb inherited in 1978  | 
|
171  | 
approximately 200 Million Dollar from his father. Mr Drumb prides  | 
|
172  | 
himself to be a brilliant business man because nowadays it is  | 
|
173  | 
estimated he is 3 Billion Dollar worth (one is not sure, of course,  | 
|
174  | 
because Mr Drumb refuses to make his tax records public).  | 
|
175  | 
||
176  | 
Since the question about Mr Drumb's business acumen remains open,  | 
|
177  | 
let's do a quick back-of-the-envelope calculation in Scala whether his  | 
|
178  | 
claim has any merit. Let's suppose we are given \$100 in 1978 and we  | 
|
179  | 
follow a really dumb investment strategy, namely:  | 
|
180  | 
||
181  | 
\begin{itemize}
 | 
|
182  | 
\item We blindly choose a portfolio of stocks, say some Blue-Chip stocks  | 
|
183  | 
or some Real Estate stocks.  | 
|
184  | 
\item If some of the stocks in our portfolio are traded in January of  | 
|
185  | 
a year, we invest our money in equal amounts in each of these  | 
|
186  | 
stocks. For example if we have \$100 and there are four stocks that  | 
|
187  | 
are traded in our portfolio, we buy \$25 worth of stocks  | 
|
188  | 
from each. Be careful to also test cases where you trade with 3 stocks, for example.  | 
|
189  | 
\item Next year in January, we look at how our stocks did, liquidate  | 
|
190  | 
everything, and re-invest our (hopefully) increased money in again  | 
|
191  | 
the stocks from our portfolio (there might be more stocks available,  | 
|
192  | 
if companies from our portfolio got listed in that year, or less if  | 
|
193  | 
some companies went bust or were de-listed).  | 
|
194  | 
\item We do this for 40 years until January 2018 and check what would  | 
|
195  | 
have become out of our \$100.  | 
|
196  | 
\end{itemize}
 | 
|
197  | 
||
198  | 
\noindent  | 
|
| 201 | 199  | 
Until Yahoo was bought by Altaba last year, historical stock market  | 
| 199 | 200  | 
data for such back-of-the-envelope calculations was freely available  | 
| 201 | 201  | 
online. Unfortunately nowadays this kind of data is more difficult to  | 
| 199 | 202  | 
obtain, unless you are prepared to pay extortionate prices or be  | 
| 201 | 203  | 
severely rate-limited. Therefore this assignment comes with a number  | 
| 199 | 204  | 
of files containing CSV-lists with the historical stock prices for the  | 
205  | 
companies in our portfolios. Use these files for the following  | 
|
206  | 
tasks.\bigskip  | 
|
207  | 
||
| 201 | 208  | 
\newpage  | 
| 199 | 209  | 
\noindent  | 
210  | 
\textbf{Tasks}
 | 
|
211  | 
||
212  | 
\begin{itemize}
 | 
|
213  | 
\item[(1)] Write a function \texttt{get\_january\_data} that takes a
 | 
|
214  | 
stock symbol and a year as arguments. The function reads the  | 
|
215  | 
corresponding CSV-file and returns the list of strings that start  | 
|
216  | 
with the given year (each line in the CSV-list is of the form  | 
|
217  | 
  \texttt{someyear-01-someday,someprice}).\hfill[1 Mark]
 | 
|
218  | 
||
219  | 
\item[(2)] Write a function \texttt{get\_first\_price} that takes
 | 
|
220  | 
again a stock symbol and a year as arguments. It should return the  | 
|
221  | 
first January price for the stock symbol in the given year. For this  | 
|
222  | 
it uses the list of strings generated by  | 
|
223  | 
  \texttt{get\_january\_data}.  A problem is that normally a stock
 | 
|
224  | 
exchange is not open on 1st of January, but depending on the day of  | 
|
225  | 
the week on a later day (maybe 3rd or 4th). The easiest way to solve  | 
|
226  | 
this problem is to obtain the whole January data for a stock symbol  | 
|
227  | 
and then select the earliest, or first, entry in this list. The  | 
|
228  | 
stock price of this entry should be converted into a double. Such a  | 
|
229  | 
price might not exist, in case the company does not exist in the given  | 
|
230  | 
year. For example, if you query for Google in January of 1980, then  | 
|
231  | 
clearly Google did not exist yet. Therefore you are asked to  | 
|
232  | 
  return a trade price with type \texttt{Option[Double]}\ldots\texttt{None}
 | 
|
233  | 
  will be the value for when no price exists; \texttt{Some} if  there is a
 | 
|
234  | 
price.\hfill[1 Mark]  | 
|
235  | 
||
236  | 
\item[(3)] Write a function \texttt{get\_prices} that takes a
 | 
|
237  | 
portfolio (a list of stock symbols), a years range and gets all the  | 
|
238  | 
first trading prices for each year in the range. You should organise  | 
|
239  | 
  this as a list of lists of \texttt{Option[Double]}'s. The inner
 | 
|
240  | 
lists are for all stock symbols from the portfolio and the outer  | 
|
241  | 
list for the years. For example for Google and Apple in years 2010  | 
|
242  | 
(first line), 2011 (second line) and 2012 (third line) you obtain:  | 
|
243  | 
||
244  | 
\begin{verbatim}
 | 
|
245  | 
List(List(Some(311.349976), Some(20.544939)),  | 
|
246  | 
List(Some(300.222351), Some(31.638695)),  | 
|
247  | 
List(Some(330.555054), Some(39.478039)))  | 
|
| 201 | 248  | 
\end{verbatim}\hfill[1 Mark]
 | 
| 199 | 249  | 
\end{itemize}
 | 
250  | 
||
251  | 
\subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)}
 | 
|
252  | 
||
253  | 
\noindent  | 
|
254  | 
\textbf{Tasks}
 | 
|
255  | 
||
256  | 
\begin{itemize}  
 | 
|
257  | 
\item[(4)] Write a function that calculates the \emph{change factor} (delta)
 | 
|
258  | 
for how a stock price has changed from one year to the next. This is  | 
|
259  | 
only well-defined, if the corresponding company has been traded in both  | 
|
260  | 
years. In this case you can calculate  | 
|
261  | 
||
262  | 
\[  | 
|
263  | 
  \frac{price_{new} - price_{old}}{price_{old}}
 | 
|
264  | 
\]  | 
|
265  | 
||
266  | 
If the change factor is defined, you should return it  | 
|
267  | 
  as \texttt{Some(change\_factor)}; if not, you should return
 | 
|
268  | 
  \texttt{None}.\mbox{}\hfill\mbox{[1 Mark]}
 | 
|
269  | 
||
270  | 
\item[(5)] Write a function that calculates all change factors  | 
|
271  | 
(deltas) for the prices we obtained under Part 2. For the running  | 
|
272  | 
example of Google and Apple for the years 2010 to 2012 you should  | 
|
273  | 
obtain 4 change factors:  | 
|
274  | 
||
275  | 
\begin{verbatim}  
 | 
|
276  | 
List(List(Some(-0.03573992567129673), Some(0.539975124774038))  | 
|
277  | 
List(Some(0.10103412653643493), Some(0.24777709700099845)))  | 
|
278  | 
\end{verbatim}
 | 
|
279  | 
||
280  | 
That means Google did a bit badly in 2010, while Apple did very well.  | 
|
281  | 
Both did OK in 2011. Make sure you handle the cases where a company is  | 
|
282  | 
  not listed in a year. In such cases the change factor should be \texttt{None}
 | 
|
283  | 
(see~(4)).  | 
|
284  | 
  \mbox{}\hfill\mbox{[1 Mark]}
 | 
|
285  | 
||
286  | 
\item[(6)] Write a function that calculates the ``yield'', or  | 
|
287  | 
balance, for one year for our portfolio. This function takes the  | 
|
288  | 
change factors, the starting balance and the year as arguments. If  | 
|
289  | 
no company from our portfolio existed in that year, the balance is  | 
|
290  | 
unchanged. Otherwise we invest in each existing company an equal  | 
|
291  | 
amount of our balance. Using the change factors computed under Task  | 
|
292  | 
2, calculate the new balance. Say we had \$100 in 2010, we would have  | 
|
293  | 
received in our running example involving Google and Apple:  | 
|
294  | 
||
295  | 
  \begin{verbatim}
 | 
|
296  | 
$50 * -0.03573992567129673 + $50 * 0.539975124774038  | 
|
297  | 
= $25.21175995513706  | 
|
298  | 
  \end{verbatim}
 | 
|
299  | 
||
300  | 
as profit for that year, and our new balance for 2011 is \$125 when  | 
|
301  | 
  converted to a \texttt{Long}.\mbox{}\hfill\mbox{[1 Mark]}
 | 
|
302  | 
||
303  | 
\item[(7)] Write a function that calculates the overall balance  | 
|
304  | 
for a range of years where each year the yearly profit is compounded to  | 
|
305  | 
the new balances and then re-invested into our portfolio.  | 
|
306  | 
For this use the function and results generated under (6).\\  | 
|
307  | 
  \mbox{}\hfill\mbox{[1 Mark]}
 | 
|
308  | 
\end{itemize}\medskip  
 | 
|
309  | 
||
310  | 
||
311  | 
||
312  | 
\noindent  | 
|
313  | 
\textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
 | 
|
314  | 
collected from the S\&P 500, one for blue-chip companies, including  | 
|
315  | 
Facebook, Amazon and Baidu; and another for listed real-estate  | 
|
316  | 
companies, whose names I have never heard of. Following the dumb  | 
|
317  | 
investment strategy from 1978 until 2018 would have turned a starting  | 
|
318  | 
balance of \$100 into roughly \$101,589 for real estate and a whopping  | 
|
319  | 
\$1,587,528 for blue chips. Note when comparing these results with your  | 
|
320  | 
own calculations: there might be some small rounding errors, which  | 
|
321  | 
when compounded lead to moderately different values.\bigskip  | 
|
322  | 
||
323  | 
||
324  | 
\noindent  | 
|
325  | 
\textbf{Moral:} Reflecting on our assumptions, we are over-estimating
 | 
|
326  | 
our yield in many ways: first, who can know in 1978 about what will  | 
|
327  | 
turn out to be a blue chip company. Also, since the portfolios are  | 
|
328  | 
chosen from the current S\&P 500, they do not include the myriad  | 
|
329  | 
of companies that went bust or were de-listed over the years.  | 
|
330  | 
So where does this leave our fictional character Mr T.~Drumb? Well, given  | 
|
331  | 
his inheritance, a really dumb investment strategy would have done  | 
|
332  | 
equally well, if not much better.\medskip  | 
|
333  | 
||
334  | 
\end{document}
 | 
|
335  | 
||
336  | 
\newpage  | 
|
| 192 | 337  | 
|
| 196 | 338  | 
This part is about web-scraping and list-processing in Scala. It uses  | 
339  | 
online data about the per-capita alcohol consumption for each country  | 
|
340  | 
(per year?), and a file containing the data about the population size of  | 
|
341  | 
each country. From this data you are supposed to estimate how many  | 
|
342  | 
litres of pure alcohol are consumed worldwide.\bigskip  | 
|
| 192 | 343  | 
|
344  | 
\noindent  | 
|
| 196 | 345  | 
\textbf{Tasks (file alcohol.scala):}
 | 
| 192 | 346  | 
|
347  | 
\begin{itemize}
 | 
|
| 196 | 348  | 
\item[(1)] Write a function that given an URL requests a  | 
349  | 
comma-separated value (CSV) list. We are interested in the list  | 
|
350  | 
from the following URL  | 
|
| 192 | 351  | 
|
352  | 
\begin{center}
 | 
|
| 196 | 353  | 
  \url{https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv}
 | 
| 192 | 354  | 
\end{center}
 | 
| 127 | 355  | 
|
| 196 | 356  | 
\noindent Your function should take a string (the URL) as input, and  | 
357  | 
produce a list of strings as output, where each string is one line in  | 
|
358  | 
the corresponding CSV-list. This list from the URL above should  | 
|
359  | 
contain 194 lines.\medskip  | 
|
| 192 | 360  | 
|
361  | 
\noindent  | 
|
| 196 | 362  | 
Write another function that can read the file \texttt{population.csv}
 | 
| 201 | 363  | 
from disk (the file is distributed with the assignment). This  | 
| 196 | 364  | 
function should take a string as argument, the file name, and again  | 
365  | 
return a list of strings corresponding to each entry in the  | 
|
366  | 
CSV-list. For \texttt{population.csv}, this list should contain 216
 | 
|
367  | 
lines.\hfill[1 Mark]  | 
|
368  | 
||
369  | 
||
370  | 
\item[(2)] Unfortunately, the CSV-lists contain a lot of ``junk'' and we  | 
|
371  | 
need to extract the data that interests us. From the header of the  | 
|
372  | 
alcohol list, you can see there are 5 columns  | 
|
373  | 
||
374  | 
  \begin{center}
 | 
|
375  | 
    \begin{tabular}{l}
 | 
|
376  | 
      \texttt{country (name),}\\
 | 
|
377  | 
      \texttt{beer\_servings,}\\
 | 
|
378  | 
      \texttt{spirit\_servings,}\\
 | 
|
379  | 
      \texttt{wine\_servings,}\\
 | 
|
380  | 
      \texttt{total\_litres\_of\_pure\_alcohol}
 | 
|
381  | 
    \end{tabular}  
 | 
|
382  | 
  \end{center}
 | 
|
383  | 
||
384  | 
\noindent  | 
|
385  | 
Write a function that extracts the data from the first column,  | 
|
386  | 
the country name, and the data from the fifth column (converted into  | 
|
387  | 
  a \texttt{Double}). For this go through each line of the CSV-list
 | 
|
388  | 
  (except the first line), use the \texttt{split(",")} function to
 | 
|
389  | 
divide each line into an array of 5 elements. Keep the data from the  | 
|
390  | 
first and fifth element in these arrays.\medskip  | 
|
| 192 | 391  | 
|
| 196 | 392  | 
\noindent  | 
393  | 
Write another function that processes the population size list. This  | 
|
394  | 
  is already of the form country name and population size.\footnote{Your
 | 
|
395  | 
friendly lecturer already did the messy processing for you from the  | 
|
396  | 
  Worldbank database, see \url{https://github.com/datasets/population/tree/master/data} for the original.} Again, split the
 | 
|
397  | 
strings according to the commas. However, this time generate a  | 
|
398  | 
  \texttt{Map} from country names to population sizes.\hfill[1 Mark]
 | 
|
399  | 
||
400  | 
\item[(3)] In (2) you generated the data about the alcohol consumption  | 
|
401  | 
per capita for each country, and also the population size for each  | 
|
402  | 
country. From this generate next a sorted(!) list of the overall  | 
|
403  | 
alcohol consumption for each country. The list should be sorted from  | 
|
404  | 
highest alcohol consumption to lowest. The difficulty is that the  | 
|
405  | 
data is scraped off from ``random'' sources on the Internet and  | 
|
406  | 
annoyingly the spelling of some country names does not always agree in both  | 
|
407  | 
lists. For example the alcohol list contains  | 
|
408  | 
  \texttt{Bosnia-Herzegovina}, while the population writes this country as
 | 
|
409  | 
  \texttt{Bosnia and Herzegovina}. In your sorted
 | 
|
410  | 
overall list include only countries from the alcohol list, whose  | 
|
411  | 
exact country name is also in the population size list. This means  | 
|
412  | 
you can ignore countries like Bosnia-Herzegovina from the overall  | 
|
413  | 
alcohol consumption. There are 177 countries where the names  | 
|
414  | 
agree. The UK is ranked 10th on this list by  | 
|
415  | 
consuming 671,976,864 Litres of pure alcohol each year.\medskip  | 
|
416  | 
||
417  | 
\noindent  | 
|
418  | 
Finally, write another function that takes an integer, say  | 
|
419  | 
  \texttt{n}, as argument. You can assume this integer is between 0
 | 
|
420  | 
and 177 (the number of countries in the sorted list above). The  | 
|
421  | 
function should return a triple, where the first component is the  | 
|
422  | 
sum of the alcohol consumption in all countries (on the list); the  | 
|
423  | 
  second component is the sum of the \texttt{n}-highest alcohol
 | 
|
424  | 
consumers on the list; and the third component is the percentage the  | 
|
425  | 
  \texttt{n}-highest alcohol consumers drink with respect to the
 | 
|
426  | 
the world consumption. You will see that according to our data, 164  | 
|
427  | 
countries (out of 177) gobble up 100\% of the World alcohol  | 
|
428  | 
  consumption.\hfill\mbox{[1 Mark]}
 | 
|
| 18 | 429  | 
\end{itemize}
 | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
430  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
9 
diff
changeset
 | 
431  | 
\noindent  | 
| 196 | 432  | 
\textbf{Hints:} useful list functions: \texttt{.drop(n)},
 | 
433  | 
\texttt{.take(n)} for dropping or taking some elements in a list,
 | 
|
434  | 
\texttt{.getLines} for separating lines in a string;
 | 
|
435  | 
\texttt{.sortBy(\_.\_2)} sorts a list of pairs according to the second
 | 
|
436  | 
elements in the pairs---the sorting is done from smallest to highest;  | 
|
437  | 
useful \texttt{Map} functions: \texttt{.toMap} converts a list of
 | 
|
438  | 
pairs into a \texttt{Map}, \texttt{.isDefinedAt(k)} tests whether the
 | 
|
439  | 
map is defined at that key, that is would produce a result when  | 
|
440  | 
called with this key; useful data functions: \texttt{Source.fromURL},
 | 
|
441  | 
\texttt{Source.fromFile} for obtaining a webpage and reading a file.
 | 
|
| 127 | 442  | 
|
| 196 | 443  | 
\newpage  | 
444  | 
||
445  | 
||
| 18 | 446  | 
|
| 129 | 447  | 
|
| 135 | 448  | 
|
| 6 | 449  | 
|
450  | 
%%% Local Variables:  | 
|
451  | 
%%% mode: latex  | 
|
452  | 
%%% TeX-master: t  | 
|
453  | 
%%% End:  |