cws/cw01.tex
changeset 266 ca48ac1d3c3e
parent 252 a9d84442fb65
child 276 52faee6d0be2
equal deleted inserted replaced
265:59779ce322a6 266:ca48ac1d3c3e
    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 assignment is about Scala and worth 10\%. The first and second
    16 This assignment is about Scala and worth 10\%. The basic
    17 part are due on \cwSIX{} at 11pm, and the third part on \cwSIXa{}
    17 part is due on \cwSIX{} at 11pm, and the main part on \cwSIXa{}
    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 main 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
    22  
    22  
    23 \IMPORTANT{}
    23 \IMPORTANT{}
    24 
    24 
    31 \subsection*{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.\medskip
    37 
    37 
       
    38 \noindent
    38 In addition, the Scala assignments come with a reference implementation
    39 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 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 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 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 function from the template file. Say you want to find out what
    63 \texttt{.toList} for conversions, \texttt{List(...).max} for the
    64 \texttt{.toList} for conversions, \texttt{List(...).max} for the
    64 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of
    65 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of
    65 a value in a list.\bigskip
    66 a value in a list.\bigskip
    66 
    67 
    67 \noindent
    68 \noindent
    68 \textbf{For Part 2 + 3:} useful string functions: \texttt{.startsWith(...)} for
    69 \textbf{For Part 2:} useful string functions:
    69 checking whether a string has a given prefix, \texttt{\_ ++ \_} for
    70 \texttt{.startsWith(...)} for checking whether a string has a given
    70 concatenating two strings; useful option functions: \texttt{.flatten}
    71 prefix, \texttt{\_ ++ \_} for concatenating two strings; useful option
    71 flattens a list of options such that it filters way all
    72 functions: \texttt{.flatten} flattens a list of options such that it
    72 \texttt{None}'s, \texttt{Try(...).getOrElse ...} runs some code that
    73 filters way all \texttt{None}'s, \texttt{Try(...).getOrElse ...} runs
    73 might raise an exception---if yes, then a default value can be given;
    74 some code that might raise an exception---if yes, then a default value
    74 useful list functions: \texttt{.head} for obtaining the first element
    75 can be given; useful list functions: \texttt{.head} for obtaining the
    75 in a non-empty list, \texttt{.length} for the length of a
    76 first element in a non-empty list, \texttt{.length} for the length of
    76 list; \texttt{.filter(...)} for filtering out elements in a list; \texttt{.getLines.toList} for obtaining a list of lines from
    77 a list; \texttt{.filter(...)} for filtering out elements in a list;
    77 a file; \texttt{.split(",").toList} for splitting strings according to
    78 \texttt{.getLines.toList} for obtaining a list of lines from a file;
    78 a comma.\bigskip
    79 \texttt{.split(",").toList} for splitting strings according to a
    79 
    80 comma.\bigskip
    80 \noindent
    81 
    81 Fortunately Scala supports operator overloading. But make sure you understand the difference between \texttt{100 / 3} and
    82 \noindent
       
    83 \textbf{Note!} Fortunately Scala supports operator overloading. But
       
    84 make sure you understand the difference between \texttt{100 / 3} and
    82 \texttt{100.0 / 3}!
    85 \texttt{100.0 / 3}!
    83 
    86 
    84 \newpage
    87 \newpage
    85 \subsection*{Part 1 (3 Marks, file collatz.scala)}
    88 \subsection*{Basic Part (3 Marks, file collatz.scala)}
    86 
    89 
    87 This part is about recursion. You are asked to implement a Scala
    90 This part is about recursion. You are asked to implement a Scala
    88 program that tests examples of the \emph{$3n + 1$-conjecture}, also
    91 program that tests examples of the \emph{$3n + 1$-conjecture}, also
    89 called \emph{Collatz conjecture}. This conjecture can be described as
    92 called \emph{Collatz conjecture}. This conjecture can be described as
    90 follows: Start with any positive number $n$ greater than $0$:
    93 follows: Start with any positive number $n$ greater than $0$:
    95   1$.
    98   1$.
    96 \item Repeat this process and you will always end up with $1$.
    99 \item Repeat this process and you will always end up with $1$.
    97 \end{itemize}
   100 \end{itemize}
    98 
   101 
    99 \noindent
   102 \noindent
   100 For example if you start with $6$, or $9$, you obtain the
   103 For example if you start with, say, $6$ and $9$, you obtain the
   101 series
   104 two series
   102 
   105 
   103 \[
   106 \[
   104 \begin{array}{@{}l@{\hspace{5mm}}l@{}}
   107 \begin{array}{@{}l@{\hspace{5mm}}l@{}}
   105 6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 8 steps)}\\
   108 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)}\\
   109 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1  & \text{(= 19 steps)}\\
   113 this will \emph{always} happen for every number greater than
   116 this will \emph{always} happen for every number greater than
   114 0.\footnote{While it is relatively easy to test this conjecture with
   117 0.\footnote{While it is relatively easy to test this conjecture with
   115   particular numbers, it is an interesting open problem to
   118   particular numbers, it is an interesting open problem to
   116   \emph{prove} that the conjecture is true for \emph{all} numbers ($>
   119   \emph{prove} that the conjecture is true for \emph{all} numbers ($>
   117   0$). Paul Erd\"o{}s, a famous mathematician you might have hard
   120   0$). Paul Erd\"o{}s, a famous mathematician you might have hard
   118   about, said about this conjecture: ``Mathematics may not be ready
   121   about, said about this conjecture: ``Mathematics may not [yet] be ready
   119   for such problems.'' and also offered a \$500 cash prize for its
   122   for such problems.'' and also offered a \$500 cash prize for its
   120   solution. Jeffrey Lagarias, another mathematician, claimed that
   123   solution. Jeffrey Lagarias, another mathematician, claimed that
   121   based only on known information about this problem, ``this is an
   124   based only on known information about this problem, ``this is an
   122   extraordinarily difficult problem, completely out of reach of
   125   extraordinarily difficult problem, completely out of reach of
   123   present day mathematics.'' There is also a
   126   present day mathematics.'' There is also a
   163   
   166   
   164 
   167 
   165 
   168 
   166 
   169 
   167 
   170 
   168 \subsection*{Part 2 (3 Marks, file drumb.scala)}
   171 \subsection*{Main Part (7 Marks, file drumb.scala)}
   169 
   172 
   170 A purely fictional character named Mr T.~Drumb inherited in 1978
   173 A purely fictional character named Mr T.~Drumb inherited in 1978
   171 approximately 200 Million Dollar from his father. Mr Drumb prides
   174 approximately 200 Million Dollar from his father. Mr Drumb prides
   172 himself to be a brilliant business man because nowadays it is
   175 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,
   176 estimated he is 3 Billion Dollar worth (one is not sure, of course,
   189 \item Next year in January, we look at how our stocks did, liquidate
   192 \item Next year in January, we look at how our stocks did, liquidate
   190   everything, and re-invest our (hopefully) increased money in again
   193   everything, and re-invest our (hopefully) increased money in again
   191   the stocks from our portfolio (there might be more stocks available,
   194   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
   195   if companies from our portfolio got listed in that year, or less if
   193   some companies went bust or were de-listed).
   196   some companies went bust or were de-listed).
   194 \item We do this for 40 years until January 2018 and check what would
   197 \item We do this for 40 years until January 2019 and check what would
   195   have become out of our \$100.
   198   have become out of our \$100.
   196 \end{itemize}
   199 \end{itemize}
   197 
   200 
   198 \noindent
   201 \noindent
   199 Until Yahoo was bought by Altaba last year, historical stock market
   202 Until Yahoo was bought by Altaba a few years ago, historical stock market
   200 data for such back-of-the-envelope calculations was freely available
   203 data for such back-of-the-envelope calculations was freely available
   201 online. Unfortunately nowadays this kind of data is more difficult to
   204 online. Unfortunately nowadays this kind of data is more difficult to
   202 obtain, unless you are prepared to pay extortionate prices or be
   205 obtain, unless you are prepared to pay extortionate prices or be
   203 severely rate-limited.  Therefore this assignment comes with a number
   206 severely rate-limited.  Therefore this assignment comes with a number
   204 of files containing CSV-lists with the historical stock prices for the
   207 of files containing CSV-lists with the historical stock prices for the
   240   lists are for all stock symbols from the portfolio and the outer
   243   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
   244   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:
   245   (first line), 2011 (second line) and 2012 (third line) you obtain:
   243 
   246 
   244 \begin{verbatim}
   247 \begin{verbatim}
   245   List(List(Some(311.349976), Some(20.544939)), 
   248   List(List(Some(312.204773), Some(26.782711)), 
   246        List(Some(300.222351), Some(31.638695)),
   249        List(Some(301.0466),   Some(41.244694)), 
   247        List(Some(330.555054), Some(39.478039)))
   250        List(Some(331.462585), Some(51.464207)))
   248 \end{verbatim}\hfill[1 Mark]
   251 \end{verbatim}\hfill[1 Mark]
   249 \end{itemize}
   252 
   250 
   253 
   251 \subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)}
   254 %\end{itemize}
   252 
   255 
   253 \noindent
   256 %\subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)}
   254 \textbf{Tasks}
   257 %
   255 
   258 %\noindent
   256 \begin{itemize}  
   259 %\textbf{Tasks}
       
   260 
       
   261 %\begin{itemize}  
       
   262 
   257 \item[(4)] Write a function that calculates the \emph{change factor} (delta)
   263 \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
   264   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
   265   only well-defined, if the corresponding company has been traded in both
   260   years. In this case you can calculate
   266   years. In this case you can calculate
   261 
   267 
   266   If the change factor is defined, you should return it
   272   If the change factor is defined, you should return it
   267   as \texttt{Some(change\_factor)}; if not, you should return
   273   as \texttt{Some(change\_factor)}; if not, you should return
   268   \texttt{None}.\mbox{}\hfill\mbox{[1 Mark]}
   274   \texttt{None}.\mbox{}\hfill\mbox{[1 Mark]}
   269   
   275   
   270 \item[(5)] Write a function that calculates all change factors
   276 \item[(5)] Write a function that calculates all change factors
   271   (deltas) for the prices we obtained under Part 2. For the running
   277   (deltas) for the prices we obtained in Task (2). For the running
   272   example of Google and Apple for the years 2010 to 2012 you should
   278   example of Google and Apple for the years 2010 to 2012 you should
   273   obtain 4 change factors:
   279   obtain 4 change factors:
   274 
   280 
   275 \begin{verbatim}  
   281 \begin{verbatim}
   276   List(List(Some(-0.03573992567129673), Some(0.539975124774038))
   282   List(List(Some(-0.03573991804411003), Some(0.539974575389325)), 
   277        List(Some(0.10103412653643493), Some(0.24777709700099845)))
   283        List(Some(0.10103414222249969), Some(0.24777764141006836)))
   278 \end{verbatim}
   284 \end{verbatim}
   279 
   285 
   280   That means Google did a bit badly in 2010, while Apple did very well.
   286   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
   287   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}
   288   not listed in a year. In such cases the change factor should be \texttt{None}
   283   (see~(4)).
   289   (recall Task~(4)).
   284   \mbox{}\hfill\mbox{[1 Mark]}
   290   \mbox{}\hfill\mbox{[1 Mark]}
   285 
   291 
   286 \item[(6)] Write a function that calculates the ``yield'', or
   292 \item[(6)] Write a function that calculates the ``yield'', or
   287   balance, for one year for our portfolio.  This function takes the
   293   balance, for one year for our portfolio.  This function takes the
   288   change factors, the starting balance and the year as arguments. If
   294   change factors, the starting balance and the year as arguments. If
   289   no company from our portfolio existed in that year, the balance is
   295   no company from our portfolio existed in that year, the balance is
   290   unchanged. Otherwise we invest in each existing company an equal
   296   unchanged. Otherwise we invest in each existing company an equal
   291   amount of our balance. Using the change factors computed under Task
   297   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
   298   (2), calculate the new balance. Say we had \$100 in 2010, we would have
   293   received in our running example involving Google and Apple:
   299   received in our running example involving Google and Apple:
   294 
   300 
   295   \begin{verbatim}
   301   \begin{verbatim}
   296   $50 * -0.03573992567129673 + $50 * 0.539975124774038
   302   $50 * -0.03573991804411003 + $50 * 0.539974575389325
   297                                        = $25.21175995513706
   303                                        = $25.21173286726075
   298   \end{verbatim}
   304   \end{verbatim}
   299 
   305 
   300   as profit for that year, and our new balance for 2011 is \$125 when
   306   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]}
   307   converted to a \texttt{Long}.\mbox{}\hfill\mbox{[1 Mark]}
   302   
   308   
   312 \noindent
   318 \noindent
   313 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
   319 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
   314 collected from the S\&P 500, one for blue-chip companies, including
   320 collected from the S\&P 500, one for blue-chip companies, including
   315 Facebook, Amazon and Baidu; and another for listed real-estate
   321 Facebook, Amazon and Baidu; and another for listed real-estate
   316 companies, whose names I have never heard of. Following the dumb
   322 companies, whose names I have never heard of. Following the dumb
   317 investment strategy from 1978 until 2018 would have turned a starting
   323 investment strategy from 1978 until 2019 would have turned a starting
   318 balance of \$100 into roughly \$101,589 for real estate and a whopping
   324 balance of \$100 into roughly \$39,162 for real estate and a whopping
   319 \$1,587,528 for blue chips.  Note when comparing these results with your
   325 \$462,199 for blue chips.  Note when comparing these results with your
   320 own calculations: there might be some small rounding errors, which
   326 own calculations: there might be some small rounding errors, which
   321 when compounded lead to moderately different values.\bigskip
   327 when compounded lead to moderately different values.\bigskip
   322 
   328 
   323 
   329 
   324 \noindent
   330 \noindent