updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Wed, 18 Nov 2020 01:54:39 +0000
changeset 363 e5c1d69cffa4
parent 362 1bde878ba6c9
child 364 f1a6fa599d26
updated
main_testing1/drumb.scala
pre_testing1/collatz.scala
progs/lecture2.scala
slides/slides02.pdf
slides/slides02.tex
--- a/main_testing1/drumb.scala	Tue Nov 17 00:34:55 2020 +0000
+++ b/main_testing1/drumb.scala	Wed Nov 18 01:54:39 2020 +0000
@@ -167,6 +167,11 @@
 
 
 
+
+investment(List("AAPL"), 2000 to 2001, 100)
+val aapl_prices = get_prices(List("AAPL"), 2000 to 2002)
+val aapl_deltas = get_deltas(aapl_prices)
+
 //test cases for the two portfolios given above
 
 //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
--- a/pre_testing1/collatz.scala	Tue Nov 17 00:34:55 2020 +0000
+++ b/pre_testing1/collatz.scala	Wed Nov 18 01:54:39 2020 +0000
@@ -1,54 +1,59 @@
-object CW6a {
+// Basic Part about the 3n+1 conjecture
+//==================================
+
+// generate jar with
+//   > scala -d collatz.jar  collatz.scala
+
+object CW6a { // for purposes of generating a jar
 
-//(1) Complete the collatz function below. It should
-//    recursively calculate the number of steps needed
-//    until the collatz series reaches the number 1.
-//    If needed, you can use an auxiliary function that
-//    performs the recursion. The function should expect
-//    arguments in the range of 1 to 1 Million.
+/*
+def collatz(n: Long): Long =
+  if (n == 1) 0 else
+    if (n % 2 == 0) 1 + collatz(n / 2) else 
+      1 + collatz(3 * n + 1)
+*/
 
-def collatz(n: Long) : Long =
-    if ( n == 1) 1;
-    else if (n % 2 == 0) 1 + collatz( n / 2);
-    else 1 + collatz( n * 3 + 1);
+def aux(n: Long, acc: Long) : Long =
+  if (n == 1) acc else
+    if (n % 2 == 0) aux(n / 2, acc + 1) else
+      aux(3 * n + 1, acc + 1)
 
 
-//(2) Complete the collatz_max function below. It should
-//    calculate how many steps are needed for each number
-//    from 1 up to a bound and then calculate the maximum number of
-//    steps and the corresponding number that needs that many
-//    steps. Again, you should expect bounds in the range of 1
-//    up to 1 Million. The first component of the pair is
-//    the maximum number of steps and the second is the
-//    corresponding number.
+def collatz(n: Long): Long = aux(n, 0)
+
+def collatz_max(bnd: Long): (Long, Long) = {
+  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
+  all.maxBy(_._1)
+}
 
-def collatz_max(bnd: Long) : (Long, Long) =
-     ((1.toLong to bnd).toList.map
-        (n => collatz(n)).max ,
-            (1.toLong to bnd).toList.map
-                (n => collatz(n)).indexOf((1.toLong to bnd).toList.map
-                    (n => collatz(n)).max) + 1);
+//collatz_max(1000000)
+//collatz_max(10000000)
+//collatz_max(100000000)
+
+/* some test cases
+val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
 
-//(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.
-//idk
- def is_pow_of_two(n: Long) : Boolean =
-    if ( n & ( n - 1) == 0) true;
-    else false;
+for (bnd <- bnds) {
+  val (steps, max) = collatz_max(bnd)
+  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
+}
+
+*/
 
-def is_hard(n: Long) : Boolean =
-    if ( (3*n + 1) & 3*n == 0) true;
-    else false;
+def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
+
+def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
+
+def last_odd(n: Long) : Long = 
+  if (is_hard(n)) n else
+    if (n % 2 == 0) last_odd(n / 2) else 
+      last_odd(3 * n + 1)
 
 
-def last_odd(n: Long) : Long = ???
+//for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}")
+//for (i <- 1 to 100) println(s"$i: ${collatz(i)}")
+
+}
 
 
 
-}
--- a/progs/lecture2.scala	Tue Nov 17 00:34:55 2020 +0000
+++ b/progs/lecture2.scala	Wed Nov 18 01:54:39 2020 +0000
@@ -1,6 +1,6 @@
 // Scala Lecture 2
 //=================
-
+ 
 
 // String Interpolations
 //=======================
@@ -272,11 +272,41 @@
 
 lst.map(square).filter(_ > 4)
 
-(lst.map(square)
+lst.map(square).find(_ > 4)
+lst.map(square).find(_ > 4).map(double)
+
+lst.map(square)
    .find(_ > 4)
-   .map(square))
+   .map(double)
+
+
+// Option Type and maps
+//======================
+
+// a function that turns strings into numbers (similar to .toInt)
+Integer.parseInt("12u34")
+
+// maps on Options
+
+import scala.util._
 
-lst.map(square).find(_ > 4)
+def get_me_an_int(s: String) : Option[Int] = 
+ Try(Some(Integer.parseInt(s))).getOrElse(None)
+
+get_me_an_int("12345").map(_ % 2 == 0)
+get_me_an_int("12u34").map(_ % 2 == 0)
+
+
+
+val lst = List("12345", "foo", "5432", "bar", "x21", "456")
+for (x <- lst) yield get_me_an_int(x)
+
+// summing up all the numbers
+
+lst.map(get_me_an_int).flatten.sum
+
+
+
 
 // this is actually how for-comprehensions are
 // defined in Scala
@@ -288,14 +318,14 @@
 // type of functions is for example Int => Int
 
 
-def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
+def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
+{
   if (lst == Nil) Nil
   else f(lst.head) :: my_map_int(lst.tail, f)
 }
 
 my_map_int(lst, square)
 
-
 // same function using pattern matching: a kind
 // of switch statement on steroids (see more later on)
 
@@ -306,87 +336,19 @@
   }
 
 
+
+val biglst = (1 to 10000).toList
+my_map_int(biglst, double)
+
+(1 to 10000000).toList.map(double)
+
 // other function types
 //
 // f1: (Int, Int) => Int
 // f2: List[String] => Option[Int]
 // ... 
-val lst = (1 to 10).toList
-
-lst.sum
-
-val lst = List(1,2,3,4)
-
-lst.head
-lst.tail
-
-def sumOf(f: Int => Int, lst: List[Int]): Int = 
-lst match {
-  case Nil => 0
-  case x::foo => f(x) + sumOf(f, foo)
-}
-
-def sum_squares(lst: List[Int]) = sumOf(square, lst)
-def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
-
-sum_squares(lst)
-sum_cubes(lst)
-
-// lets try a factorial
-def fact(n: Int) : Int = 
-  if (n == 0) 1 else n * fact(n - 1)
-
-def sum_fact(lst: List[Int]) = sumOf(fact, lst)
-sum_fact(lst)
-
 
 
-// sometimes it is needed that you specify the type. 
-(1 to 100).filter((x: Int) => x % 2 == 0).sum 
-
-// in this case it is clear that x must be an Int
-(1 to 100).filter(x => x % 2 == 0).sum
-
-// When each parameter (only x in this case) is used only once
-// you can use the wizardy placeholder syntax
-(1 to 100).filter(_ % 2 == 0).sum
-
-
-
-// Option Type and maps
-//======================
-
-// a function that turns strings into numbers (similar to .toInt)
-Integer.parseInt("12u34")
-
-import scala.util._
-
-def get_me_an_int(s: String) : Option[Int] = 
- Try(Some(Integer.parseInt(s))).getOrElse(None)
-
-val lst = List("12345", "foo", "5432", "bar", "x21", "456")
-for (x <- lst) yield get_me_an_int(x)
-
-// summing up all the numbers
-
-lst.map(get_me_an_int).flatten.sum
-lst.map(get_me_an_int).flatten.sum
-
-lst.flatMap(get_me_an_int).sum
-
-// maps on Options
-
-get_me_an_int("12345").map(even)
-get_me_an_int("12u34").map(even)
-
-def my_map_option(o: Option[Int], f : Int => Int) : Option[Int] = {
-o match {
-   case None => None
-   case Some(foo) => Some(f(foo))
-}}
-
-my_map_option(Some(4), square)
-my_map_option(None, square)
 
 
 
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex	Tue Nov 17 00:34:55 2020 +0000
+++ b/slides/slides02.tex	Wed Nov 18 01:54:39 2020 +0000
@@ -79,53 +79,53 @@
 % \end{frame}
 
 
-% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-% \begin{frame}[t]
-% \frametitle{For-Comprehensions}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[t]
+\frametitle{For-Comprehensions}
 
-% \begin{center}
-%   \begin{tikzpicture}[scale=1,
-%                       node/.style={
-%                       rectangle,rounded corners=3mm,
-%                       very thick,draw=black!50,
-%                       minimum height=18mm, minimum width=20mm,
-%                       top color=white,bottom color=black!20}]
+\begin{center}
+  \begin{tikzpicture}[scale=1,
+                      node/.style={
+                      rectangle,rounded corners=3mm,
+                      very thick,draw=black!50,
+                      minimum height=18mm, minimum width=20mm,
+                      top color=white,bottom color=black!20}]
 
-%   \node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
-%   \node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
-%   \node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
-%   \node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
-%   \node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
-%   \node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
-%   \node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
+  \node (A0) at (0.1,0) {\texttt{\textcolor{purple}{\textbf{for}} (\alert<2->{n} <- List(}};
+  \node (A1) at (2.3,0) {\texttt{\phantom{,}1,}};
+  \node (A2) at (3.2,0) {\texttt{\phantom{,}2,}};
+  \node (A3) at (4.1,0) {\texttt{\phantom{,}3,}};
+  \node (A4) at (5.0,0) {\texttt{\phantom{,}4,}};
+  \node (A5) at (5.9,0) {\texttt{\phantom{))}5))}};
+  \node (A6) at (8,0) {\texttt{\textcolor{purple}{\textbf{yield}} \alert<2->{n\,*\,n}}};
 
-%   \onslide<2->{
-%   \node (B0) at (1.4,-3) {\texttt{List(}};
-%   \node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
-%   \node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
-%   \node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
-%   \node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
-%   \node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
+  \onslide<2->{
+  \node (B0) at (1.4,-3) {\texttt{List(}};
+  \node (B1) at (2.3,-3) {\texttt{\phantom{,}1,}};
+  \node (B2) at (3.6,-3) {\texttt{\phantom{,}4,}};
+  \node (B3) at (4.9,-3) {\texttt{\phantom{,}9,}};
+  \node (B4) at (6.2,-3) {\texttt{\phantom{,}16,}};
+  \node (B5) at (7.5,-3) {\texttt{\phantom{,}25)}};}
 
-%   \onslide<2->{
-%   \draw [->,line width=1mm] (A1.south) -- (B1.north);
-%   \draw [->,line width=1mm] (A2.south) -- (B2.north);
-%   \draw [->,line width=1mm] (A3.south) -- (B3.north);
-%   \draw [->,line width=1mm] (A4.south) -- (B4.north);
-%   \draw [->,line width=1mm] (A5.south) -- (B5.north);}
+  \onslide<2->{
+  \draw [->,line width=1mm] (A1.south) -- (B1.north);
+  \draw [->,line width=1mm] (A2.south) -- (B2.north);
+  \draw [->,line width=1mm] (A3.south) -- (B3.north);
+  \draw [->,line width=1mm] (A4.south) -- (B4.north);
+  \draw [->,line width=1mm] (A5.south) -- (B5.north);}
 
-%   \onslide<2->{
-%   \node (Q1) at (-0.45,-0.1) {};
-%   \node (Q2) at (-0.45,-2.8) {};
-%   \node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
-%   \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
-%  \end{tikzpicture}
-% \end{center}
+  \onslide<2->{
+  \node (Q1) at (-0.45,-0.1) {};
+  \node (Q2) at (-0.45,-2.8) {};
+  \node (Q3) at (-0.45,-2.95) {\alert<2->{\texttt{n\,*\,n:}}};
+  \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);}
+ \end{tikzpicture}
+\end{center}
 
-% \onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
+\onslide<3>{This is for when the for-comprehension\\ \textbf{yields / produces} a result.}
 
-% \end{frame}
-% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 
 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
@@ -529,10 +529,12 @@
      child {node {$[4,2,0,1]$\ldots}};
 \end{tikzpicture}
 \end{center}
-
+\end{frame}
 
+\begin{frame}<1-10>[t]
+  
+\end{frame}
 
-\end{frame}
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%