# HG changeset patch # User Christian Urban # Date 1602580881 -3600 # Node ID c8fcc0e0a57f74c468bffc185fea75559b8917b4 # Parent aa47abb9e723fa77323315cc19f11b4ec7989166 updated diff -r aa47abb9e723 -r c8fcc0e0a57f cws/cw01.pdf Binary file cws/cw01.pdf has changed diff -r aa47abb9e723 -r c8fcc0e0a57f cws/cw01.tex --- a/cws/cw01.tex Sat Aug 29 16:05:59 2020 +0100 +++ b/cws/cw01.tex Tue Oct 13 10:21:21 2020 +0100 @@ -8,7 +8,7 @@ \begin{document} -\section*{Preliminary Part 6 (Scala)} +\section*{Preliminary Part 6 (Scala, 3 Marks)} \mbox{}\hfill\textit{``The most effective debugging tool is still careful thought,}\\ \mbox{}\hfill\textit{coupled with judiciously placed print statements.''}\smallskip\\ @@ -241,7 +241,7 @@ \texttt{Map} from country names to population sizes.\hfill[1 Mark] \item[(3)] In (2) you generated the data about the alcohol consumption - per capita for each country, and also the population size for each + per-capita for each country, and also the population size for each country. From this generate next a sorted(!) list of the overall alcohol consumption for each country. The list should be sorted from highest alcohol consumption to lowest. The difficulty is that the diff -r aa47abb9e723 -r c8fcc0e0a57f cws/pcw01.tex --- a/cws/pcw01.tex Sat Aug 29 16:05:59 2020 +0100 +++ b/cws/pcw01.tex Tue Oct 13 10:21:21 2020 +0100 @@ -8,7 +8,7 @@ \begin{document} -\section*{Core Part 6 (Scala)} +\section*{Core Part 6 (Scala, 7 Marks)} \IMPORTANT{This part is about Scala. It is due on \cwSIXa{} at 4pm and worth 7\%.} diff -r aa47abb9e723 -r c8fcc0e0a57f handouts/pep-ho.pdf Binary file handouts/pep-ho.pdf has changed diff -r aa47abb9e723 -r c8fcc0e0a57f handouts/pep-ho.tex --- a/handouts/pep-ho.tex Sat Aug 29 16:05:59 2020 +0100 +++ b/handouts/pep-ho.tex Tue Oct 13 10:21:21 2020 +0100 @@ -91,6 +91,9 @@ %% %% Section 10 about strings; interpolations and multiline strings +% Easy installation +%https://alexarchambault.github.io/posts/2020-09-21-cs-setup.html + % Exact colors from NB \usepackage[breakable]{tcolorbox} @@ -1617,6 +1620,67 @@ %\subsection*{Coursework} +\subsection*{Scala Syntax for Java Developers} + +Scala compiles to the JVM, like the Java language. Because of this, +it can re-use many libraries. + +\begin{lstlisting}[language=Java] +Drink coke = getCoke(); +\end{lstlisting} + +\begin{lstlisting}[language=Scala] +val coke : Drink = getCoke() +\end{lstlisting} + +Unit means void: + +\begin{lstlisting}[language=Java] +public void output(String s) { + System.out.println(s); +} +\end{lstlisting} + +\begin{lstlisting}[language=Scala] +def output(s: String): Unit = println(s) +\end{lstlisting} + + +Type for list of Strings: + +\begin{lstlisting}[language=Java] +List +\end{lstlisting} + +\begin{lstlisting}[language=Scala] +List[String] +\end{lstlisting} + +String interpolations + +\begin{lstlisting}[language=Java] +System.out.println("Hello, "+ firstName + " "+ lastName + "!"); +\end{lstlisting} + +\begin{lstlisting}[language=Scala] +println(s"Hello, $firstName $lastName!") +\end{lstlisting} + + +Java provides syntactic sugar when constructing lambda functions: + +\begin{lstlisting}[language=Java] +list.foreach(item -> System.out.println("* " + item)); +\end{lstlisting} + +In Scala, we use the => symbol with anonymous functions: + +\begin{lstlisting}[language=Scala] +list.foreach(item => println(s"* $item")) +\end{lstlisting} + +new / vs case classes + \subsection*{More Info} diff -r aa47abb9e723 -r c8fcc0e0a57f marking1/collatz_test.sh --- a/marking1/collatz_test.sh Sat Aug 29 16:05:59 2020 +0100 +++ b/marking1/collatz_test.sh Tue Oct 13 10:21:21 2020 +0100 @@ -88,7 +88,7 @@ if (scala_assert "collatz.scala" "collatz_test1.scala") then echo " --> success" | tee -a $out - marks=$(( marks + 2 )) + marks=$(( marks + 1 )) else echo " --> one of the tests failed" | tee -a $out fi @@ -117,11 +117,28 @@ fi fi +### last-odd tests + +if [ $tsts -eq 0 ] +then + echo " last_odd(113) == 85" | tee -a $out + echo " last_odd(84) == 21" | tee -a $out + echo " last_odd(605) == 341" | tee -a $out + + if (scala_assert "collatz.scala" "collatz_test3.scala") + then + echo " --> success" | tee -a $out + marks=$(( marks + 1 )) + else + echo " --> one of the tests failed" | tee -a $out + fi +fi + ## final marks echo >> $out -echo "Overall mark for the Basic Part" | tee -a $out +echo "Overall mark for the Preliminary Part" | tee -a $out echo " $marks" | tee -a $out diff -r aa47abb9e723 -r c8fcc0e0a57f marking1/collatz_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/marking1/collatz_test3.scala Tue Oct 13 10:21:21 2020 +0100 @@ -0,0 +1,4 @@ + +assert(CW6a.last_odd(113) == 85) +assert(CW6a.last_odd(84) == 21) +assert(CW6a.last_odd(605) == 341) diff -r aa47abb9e723 -r c8fcc0e0a57f progs/lecture3.scala --- a/progs/lecture3.scala Sat Aug 29 16:05:59 2020 +0100 +++ b/progs/lecture3.scala Tue Oct 13 10:21:21 2020 +0100 @@ -34,6 +34,23 @@ l.map(test).flatten +// naive quicksort with "On" function + +def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = { + if (xs.size < 2) xs + else { + val pivot = xs.head + val (left, right) = xs.partition(f(_) < f(pivot)) + sortOn(f, left) ::: pivot :: sortOn(f, right.tail) + } +} + +sortOn(identity, List(99,99,99,98,10,-3,2)) +sortOn(n => - n, List(99,99,99,98,10,-3,2)) + + + + // Recursion Again ;o) //==================== diff -r aa47abb9e723 -r c8fcc0e0a57f solutions-resit/resit-sol.scala --- a/solutions-resit/resit-sol.scala Sat Aug 29 16:05:59 2020 +0100 +++ b/solutions-resit/resit-sol.scala Tue Oct 13 10:21:21 2020 +0100 @@ -167,7 +167,7 @@ // Romania, Slovakia, United Kingdom, Norway, Pakistan, Indonesia, // Malaysia, Turkey, Portugal, Hungary) // -// get_countries(recs) => +// get_countries_numbers(recs) => // // Map("" -> 4, Poland -> 2, Lebanon -> 1, Trinidad and Tobago -> 2, // Japan -> 1, Spain -> 1, Nigeria -> 1, Peru -> 1, India -> 6, diff -r aa47abb9e723 -r c8fcc0e0a57f solutions-resit/resit.scala --- a/solutions-resit/resit.scala Sat Aug 29 16:05:59 2020 +0100 +++ b/solutions-resit/resit.scala Tue Oct 13 10:21:21 2020 +0100 @@ -151,7 +151,7 @@ // Romania, Slovakia, United Kingdom, Norway, Pakistan, Indonesia, // Malaysia, Turkey, Portugal, Hungary) // -// get_countries(recs) => +// get_countries_numbers(recs) => // // Map("" -> 4, Poland -> 2, Lebanon -> 1, Trinidad and Tobago -> 2, // Japan -> 1, Spain -> 1, Nigeria -> 1, Peru -> 1, India -> 6, diff -r aa47abb9e723 -r c8fcc0e0a57f style.sty --- a/style.sty Sat Aug 29 16:05:59 2020 +0100 +++ b/style.sty Tue Oct 13 10:21:21 2020 +0100 @@ -1,16 +1,18 @@ -%\usepackage{xcolor} +\usepackage{xcolor} %%\usepackage{fontspec} \usepackage[sc]{mathpazo} \usepackage{fontspec} \setmainfont[Ligatures=TeX]{Palatino Linotype} \usepackage{amssymb} \usepackage{amsmath} -\usepackage{menukeys} \definecolor{darkblue}{rgb}{0,0,0.6} \usepackage[colorlinks=true,urlcolor=darkblue,linkcolor=darkblue]{hyperref} \usepackage{marginnote} \usepackage{fontawesome5} +%% does not work after the last upgrade +%%\usepackage{menukeys} + %%% for regular expressions and values \newcommand{\ZERO}{\mbox{\bf 0}} \newcommand{\ONE}{\mbox{\bf 1}} diff -r aa47abb9e723 -r c8fcc0e0a57f templates1/collatz.scala --- a/templates1/collatz.scala Sat Aug 29 16:05:59 2020 +0100 +++ b/templates1/collatz.scala Tue Oct 13 10:21:21 2020 +0100 @@ -1,5 +1,5 @@ -// Basic Part about the 3n+1 conjecture -//====================================== +// Preliminary Part about the 3n+1 conjecture +//============================================ object CW6a { @@ -10,7 +10,7 @@ // performs the recursion. The function should expect // arguments in the range of 1 to 1 Million. -//def collatz(n: Long) : Long = ... +def collatz(n: Long) : Long = ??? //(2) Complete the collatz_max function below. It should @@ -22,7 +22,22 @@ // the maximum number of steps and the second is the // corresponding number. -//def collatz_max(bnd: Long) : (Long, Long) = ... +def collatz_max(bnd: Long) : (Long, Long) = ??? + +//(3) Implement a function that calculates the last_odd +// number in a collatz series. For this implement an +// is_pow_of_two function which tests whether a number +// is a power of two. The function is_hard calculates +// whether 3n + 1 is a power of two. Again you can +// assume the input ranges between 1 and 1 Million, +// and also assume that the input of last_odd will not +// be a power of 2. + +def is_pow_of_two(n: Long) : Boolean = ??? + +def is_hard(n: Long) : Boolean = ??? + +def last_odd(n: Long) : Long = ??? } diff -r aa47abb9e723 -r c8fcc0e0a57f testing1/collatz_test.sh --- a/testing1/collatz_test.sh Sat Aug 29 16:05:59 2020 +0100 +++ b/testing1/collatz_test.sh Tue Oct 13 10:21:21 2020 +0100 @@ -100,3 +100,18 @@ fi +### last-odd tests + +if [ $tsts -eq 0 ] +then + echo -e " last_odd(113) == 85" >> $out + echo -e " last_odd(84) == 21" >> $out + echo -e " last_odd(605) == 341" >> $out + + if (scala_assert "collatz.scala" "collatz_test3.scala") + then + echo -e " --> success" >> $out + else + echo -e " --> ONE OF THE TESTS FAILED\n" >> $out + fi +fi diff -r aa47abb9e723 -r c8fcc0e0a57f testing1/collatz_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing1/collatz_test3.scala Tue Oct 13 10:21:21 2020 +0100 @@ -0,0 +1,4 @@ + +assert(CW6a.last_odd(113) == 85) +assert(CW6a.last_odd(84) == 21) +assert(CW6a.last_odd(605) == 341)