slides/slides03.tex
changeset 320 cdfb2ce30a3d
parent 218 22705d22c105
child 323 1f8005b4cdf6
--- a/slides/slides03.tex	Tue Nov 12 10:47:27 2019 +0000
+++ b/slides/slides03.tex	Tue Nov 19 00:40:27 2019 +0000
@@ -1,8 +1,10 @@
+% !TEX program = xelatex
 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
 \usepackage{../slides}
 \usepackage{../graphics}
 \usepackage{../langs}
 %%\usepackage{../data}
+\usetikzlibrary{shapes}
 \usepackage[export]{adjustbox}
 
 \hfuzz=220pt 
@@ -21,6 +23,22 @@
 % beamer stuff 
 \renewcommand{\slidecaption}{PEP (Scala) 03, King's College London}
 
+\newcommand{\UParrow}[3]{%
+\begin{textblock}{0}(#2,#3)%
+\onslide<#1>{%
+\begin{tikzpicture}%
+\node at (0,0) [single arrow, shape border rotate=90, fill=red,text=red]{a};%
+\end{tikzpicture}}%
+\end{textblock}}
+
+\newcommand{\DOWNarrow}[3]{%
+\begin{textblock}{0}(#2,#3)%
+\onslide<#1>{%
+\begin{tikzpicture}%
+\node at (0,0) [single arrow, shape border rotate=270, fill=red,text=red]{a};%
+\end{tikzpicture}}%
+\end{textblock}}
+
 \begin{document}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[t]
@@ -34,39 +52,33 @@
   \begin{center}
   \begin{tabular}{ll}
     Email:  & christian.urban at kcl.ac.uk\\
-    Office: & N7.07 (North Wing, Bush House)\\
-    Slides \& Code: & KEATS\medskip\\
-    Office Hours: &  \alert{next Monday} 11 -- 12 \& 13 -- 14\\
+    Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
+    Slides \& Code: & KEATS\bigskip\\
+    Office Hours: &  Thursdays 12:00 -- 14:00\\
+    Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
   \end{tabular}
   \end{center}
 
-
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c]
+\frametitle{Preliminary 6}
 
-\begin{frame}[c]
-\frametitle{Marks for CW6 (Part 1 + 2)}
-
-Raw marks (234 submissions):
+Raw marks (261 submissions):\bigskip
 
 \begin{itemize}
-\item 6\%: \hspace{4mm}163 students
-\item 5\%: \hspace{4mm}29
-\item 4\%: \hspace{4mm}3
-\item 3\%: \hspace{4mm}13
-\item 2\%: \hspace{4mm}3
+\item 3\%: \hspace{4mm}219
+\item 2\%: \hspace{4mm}19
 \item 1\%: \hspace{4mm}0
-\item 0\%: \hspace{4mm}23 
+\item 0\%: \hspace{4mm}23 \;(4 no submission)
 \end{itemize}  
 \end{frame}
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c,fragile]
 
@@ -86,65 +98,139 @@
 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
 def collatz(n: Long) : Long =
   collatzHelper(n, 0)
-\end{lstlisting}\pause
-
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-
-\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
-def collatz_max(bnd: Long) : (Long,Long) = {val lst = for(a<-(1 to bnd.toInt)) yield (collatz(a),a.toLong);val lst2 = lst.sortBy(_._1);lst2(lst2.length-1)}
-\end{lstlisting}\bigskip
-
-\tiny
-\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
-def collatz_max(bnd: Long) : (Long,Long) = {val lst = for(a<-(1 to bnd.toInt)) yield (collatz(a),a.toLong);val lst2 = lst.sortBy(_._1);lst2(lst2.length-1)}
-\end{lstlisting}\pause
-
+\end{lstlisting}
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c,fragile]
+\frametitle{Default Arguments}
 
 \small
-\begin{lstlisting}[language=Scala, xleftmargin=-4mm,numbers=left]
- def process_ratings(lines: List[String]) = {
-    val values = List[(String,String)]()
+\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
+def collatzHelper(n: Int, a: Int = 0) : Int = ...
 
-    for(line <- lines) {
-        val splitList = ...
 
-        if(splitList(2).toInt >= 4){
-            val userID = splitList(0)
-            val movieID = splitList(1)
-            val tuple = (userID, movieID)
-            tuple :: values
-        }
-    }
-    
-    values
- }
+collatzHelper(n, 3)
+collatzHelper(n, 0)
+
+collatzHelper(n)   // a = 0   
 \end{lstlisting}
 
-\normalsize
-What does this function (always) return?
-
+\DOWNarrow{1}{10.7}{3.4}
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Last Week: Options \& HO Funs.}
 
+\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
+List(7,2,3,4,5,6).find(_ < 4)
+res: Option[Int] = Some(2)
+ 
+
+List(5,6,7,8,9).find(_ < 4)
+res: Option[Int] = None
+
+
+List(1,2,3,4,5).map(x => x * x)
+res: List[Int] = List(1, 4, 9, 16, 25)
+\end{lstlisting}
+  
+  \end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Web-Crawler (1)}
+
+\small
+\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-7mm]
+def get_page(url: String) : String = {
+Try(fromURL(url)("ISO-8859-1").take(10000).mkString)
+   .getOrElse { println(s" Problem with: $url"); ""}
+}
+\end{lstlisting}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Web-Crawler (2)}
+
+\small
+\begin{lstlisting}[language=Scala, numbers=none, 
+                    xleftmargin=-7mm, escapeinside={(*@}{@*)}]
+val http_pattern = """(*@\textcolor{codegreen}{"}@*)https?://[\^(*@\textcolor{codegreen}{"}@*)]*(*@\textcolor{codegreen}{"}@*)""".r
+val email_pattern = 
+ """([a-z\d\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
+
+
+def unquote(s: String) = s.drop(1).dropRight(1)
+
+
+def get_all_URLs(page: String): Set[String] = 
+  http_pattern.findAllIn(page).map(unquote).toSet
+
+  // returns all URLs in a page  
+\end{lstlisting}
+  
+  \end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Web-Crawler (3)}
+
+\small
+\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
+def crawl(url: String, n: Int) : Unit = {
+  if (n == 0) ()
+  else {
+    println(s"  Visiting: $n $url")
+    val page = get_page(url)
+    for (u <- get_all_URLs(page)) 
+      crawl(u, n - 1)
+  }
+}
+\end{lstlisting}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Email Harvester}
+
+\small
+\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
+def emails(url: String, n: Int) : Set[String] = {
+ if (n == 0) Set()
+ else {
+  println(s"  Visiting: $n $url")
+  val page = get_page(url)
+  val new_emails = 
+    email_pattern.findAllIn(page).toSet
+  new_emails ++ 
+    (for (u <- get_all_URLs(page)) 
+       yield emails(u, n - 1)).flatten
+ }
+} 
+
+\end{lstlisting}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 \begin{frame}[c]
 \frametitle{Jumping Towers}
 
 \begin{center}
-\begin{tikzpicture}[scale=1.2]
+\begin{tikzpicture}[scale=1.3]
   \draw[line width=1mm,cap=round] (0,0) -- (5,0);
   \draw[line width=1mm,cap=round] (0,1) -- (5,1);
 
@@ -186,9 +272,7 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-
 \begin{frame}[c]
 \frametitle{``Children'' / moves}
 
@@ -205,14 +289,38 @@
 \end{tikzpicture}
 \end{center}
 
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Reverse Polish Notation}
+
+{\Large\bl{$(3 + 1) * (2 + 9)$}}\bigskip
+
+{\Large$\Rightarrow$}\bigskip
+
+{\;\;\Large\bl{$3\;\;1\;+\;2\;\;9\;+\;*$}}
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ 
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+\frametitle{Sudoku}
+
+A very simple-minded version on 110 problems:\bigskip
+
+\begin{itemize}
+\item 1 core: 800 secs
+\item 2 cores: 400 secs
+\item 8 cores: 290 secs
+\item 18 cores: 142 secs
+\end{itemize}
 
 \end{frame}
-
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-
+ 
 
 \end{document}