cws/cw01.tex
changeset 135 077e63e96287
parent 129 b1a51285de7e
child 139 3350cc06804b
--- a/cws/cw01.tex	Wed Nov 08 16:08:16 2017 +0000
+++ b/cws/cw01.tex	Wed Nov 08 20:27:56 2017 +0000
@@ -238,7 +238,8 @@
 useful \texttt{Map} functions: \texttt{.toMap} converts a list of
 pairs into a \texttt{Map}, \texttt{.isDefinedAt(k)} tests whether the
 map is defined at that key, that is would produce a result when
-called with this key.
+called with this key; useful data functions: \texttt{Source.fromURL},
+\texttt{Source.fromFile} for obtaining a webpage and reading a file.
 
 \newpage
 
@@ -274,44 +275,46 @@
 
 \noindent
 Until Yahoo was bought by Altaba this summer, historical stock market
-data was available online for free, but nowadays this kind of data is
-difficult to obtain unless you are prepared to pay extortionate prices
-or be severely rate-limited.  Therefore this coursework comes with a
-number of files containing CSV-lists about stock prices of
-various companies. Use these files for the following tasks.\bigskip
+data for such back-of-the-envelope calculations was available online
+for free, but nowadays this kind of data is difficult to obtain, unless
+you are prepared to pay extortionate prices or be severely
+rate-limited.  Therefore this coursework comes with a number of files
+containing CSV-lists about historical stock prices for companies of
+our portfolio. Use these files for the following tasks.\bigskip
 
 \noindent
 \textbf{Tasks (file drumb.scala):}
 
 \begin{itemize}
 \item[(1.a)] Write a function \texttt{get\_january\_data} that takes a
-  stock symbol and a year as argument. The function reads the
+  stock symbol and a year as arguments. The function reads the
   corresponding CSV-file and returns the list of strings that start
   with the given year (each line in the CSV-list is of the form
-  \texttt{year-01-someday,someprice}.
+  \texttt{year-01-someday,someprice}).
 
 \item[(1.b)] Write a function \texttt{get\_first\_price} that takes
-  again stock symbol and a year as arguments. It should return the
-  first January price for the stock symbol in the year. For this it
-  obtains the list of strings generated by
+  again a stock symbol and a year as arguments. It should return the
+  first January price for the stock symbol in given the year. For this
+  it uses the list of strings generated by
   \texttt{get\_january\_data}.  A problem is that normally a stock
   exchange is not open on 1st of January, but depending on the day of
   the week on a later day (maybe 3rd or 4th). The easiest way to solve
   this problem is to obtain the whole January data for a stock symbol
-  and then select the earliest entry in this list. This entry should
-  be converted into a double.  Such a price might not exist, if the
-  company does not exist in the given year. For example, if you query
-  for Google in January of 1980, then clearly Google did not exists
-  yet.  Therefore you are asked to return a trade price as
-  \texttt{Option[Double]}.
+  and then select the earliest, or first, entry in this list. The
+  stock price of this entry should be converted into a double.  Such a
+  price might not exist, in case the company does not exist in the given
+  year. For example, if you query for Google in January of 1980, then
+  clearly Google did not exists yet.  Therefore you are asked to
+  return a trade price as \texttt{Option[Double]}\ldots\texttt{None}
+  will be the value for when no price exists.
 
 \item[(1.c)] Write a function \texttt{get\_prices} that takes a
   portfolio (a list of stock symbols), a years range and gets all the
-  first trading prices for each year. You should organise this as a
-  list of lists of \texttt{Option[Double]}'s. The inner lists are for
-  all stock symbols from the portfolio and the outer list for the
-  years.  For example for Google and Apple in years 2010 (first line),
-  2011 (second line) and 2012 (third line) you obtain:
+  first trading prices for each year in the range. You should organise
+  this as a list of lists of \texttt{Option[Double]}'s. The inner
+  lists are for all stock symbols from the portfolio and the outer
+  list for the years.  For example for Google and Apple in years 2010
+  (first line), 2011 (second line) and 2012 (third line) you obtain:
 
 \begin{verbatim}
   List(List(Some(311.349976), Some(27.505054)), 
@@ -340,7 +343,8 @@
 \end{verbatim}
 
   That means Google did a bit badly in 2010, while Apple did very well.
-  Both did OK in 2011.\hfill\mbox{[1 Mark]}
+  Both did OK in 2011.\\
+  \mbox{}\hfill\mbox{[1 Mark]}
 
 \item[(3.a)] Write a function that calculates the ``yield'', or
   balance, for one year for our portfolio.  This function takes the
@@ -349,7 +353,7 @@
   unchanged. Otherwise we invest in each existing company an equal
   amount of our balance. Using the change factors computed under Task
   2, calculate the new balance. Say we had \$100 in 2010, we would have
-  received in our running example
+  received in our running example involving Google and Apple:
 
   \begin{verbatim}
   $50 * -0.03573992567129673 + $50 * 0.5399749442411563
@@ -361,7 +365,8 @@
   
 \item[(3.b)] Write a function that calculates the overall balance
   for a range of years where each year the yearly profit is compounded to
-  the new balances and then re-invested into our portfolio.\mbox{}\hfill\mbox{[1 Mark]}
+  the new balances and then re-invested into our portfolio.\\
+  \mbox{}\hfill\mbox{[1 Mark]}
 \end{itemize}\medskip  
 
 \noindent
@@ -370,10 +375,22 @@
 Facebook, Amazon and Baidu; and another for listed real-estate
 companies, whose names I have never heard of. Following the dumb
 investment strategy from 1978 until 2017 would have turned a starting
-balance of \$100 into roughly \$30,895 for real estate and a whopping
-\$188,172 for blue chips.  Note when comparing these results with your
-own results: there might be some small rounding errors, which when
-compounded, lead to moderately different values.\medskip
+balance of \$100 into roughly \$30,839 for real estate and a whopping
+\$349,597 for blue chips.  Note when comparing these results with your
+own calculations: there might be some small rounding errors, which
+when compounded, lead to moderately different values.\bigskip
+
+\noindent
+\textbf{Hints:} useful string functions: \texttt{startsWith(...)} for
+testing a string having a given prefix, \texttt{\_ + \_} for
+concatenating twop strings; useful option functions: \texttt{.flatten}
+flattens a list of options such that it filters way all
+\texttt{None}'s, \texttt{Try(...) getOrElse ...} runs some code that
+might raise an exception, if yes, then a default value can be given;
+useful list functions: \texttt{.head} for obtaining the first element
+in a non-empty list, \texttt{.length} for the length of a
+list.\bigskip
+
 
 \noindent
 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
@@ -385,8 +402,7 @@
 his inheritance, a really dumb investment strategy would have done
 equally well, if not much better.\medskip
 
-\noindent
-\textbf{Hints:}
+
 \end{document}
 
 %%% Local Variables: