slides/slides03.tex
changeset 320 cdfb2ce30a3d
parent 218 22705d22c105
child 323 1f8005b4cdf6
equal deleted inserted replaced
319:b84ea52bfd8f 320:cdfb2ce30a3d
       
     1 % !TEX program = xelatex
     1 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
     2 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
     2 \usepackage{../slides}
     3 \usepackage{../slides}
     3 \usepackage{../graphics}
     4 \usepackage{../graphics}
     4 \usepackage{../langs}
     5 \usepackage{../langs}
     5 %%\usepackage{../data}
     6 %%\usepackage{../data}
       
     7 \usetikzlibrary{shapes}
     6 \usepackage[export]{adjustbox}
     8 \usepackage[export]{adjustbox}
     7 
     9 
     8 \hfuzz=220pt 
    10 \hfuzz=220pt 
     9 
    11 
    10 %\setmonofont[Scale=.88]{Consolas}
    12 %\setmonofont[Scale=.88]{Consolas}
    18 
    20 
    19 \newcommand{\bl}[1]{\textcolor{blue}{#1}}     
    21 \newcommand{\bl}[1]{\textcolor{blue}{#1}}     
    20 
    22 
    21 % beamer stuff 
    23 % beamer stuff 
    22 \renewcommand{\slidecaption}{PEP (Scala) 03, King's College London}
    24 \renewcommand{\slidecaption}{PEP (Scala) 03, King's College London}
       
    25 
       
    26 \newcommand{\UParrow}[3]{%
       
    27 \begin{textblock}{0}(#2,#3)%
       
    28 \onslide<#1>{%
       
    29 \begin{tikzpicture}%
       
    30 \node at (0,0) [single arrow, shape border rotate=90, fill=red,text=red]{a};%
       
    31 \end{tikzpicture}}%
       
    32 \end{textblock}}
       
    33 
       
    34 \newcommand{\DOWNarrow}[3]{%
       
    35 \begin{textblock}{0}(#2,#3)%
       
    36 \onslide<#1>{%
       
    37 \begin{tikzpicture}%
       
    38 \node at (0,0) [single arrow, shape border rotate=270, fill=red,text=red]{a};%
       
    39 \end{tikzpicture}}%
       
    40 \end{textblock}}
    23 
    41 
    24 \begin{document}
    42 \begin{document}
    25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    26 \begin{frame}[t]
    44 \begin{frame}[t]
    27 \frametitle{%
    45 \frametitle{%
    32 
    50 
    33   \normalsize
    51   \normalsize
    34   \begin{center}
    52   \begin{center}
    35   \begin{tabular}{ll}
    53   \begin{tabular}{ll}
    36     Email:  & christian.urban at kcl.ac.uk\\
    54     Email:  & christian.urban at kcl.ac.uk\\
    37     Office: & N7.07 (North Wing, Bush House)\\
    55     Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
    38     Slides \& Code: & KEATS\medskip\\
    56     Slides \& Code: & KEATS\bigskip\\
    39     Office Hours: &  \alert{next Monday} 11 -- 12 \& 13 -- 14\\
    57     Office Hours: &  Thursdays 12:00 -- 14:00\\
       
    58     Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
    40   \end{tabular}
    59   \end{tabular}
    41   \end{center}
    60   \end{center}
    42 
    61 
    43 
    62 \end{frame}
    44 \end{frame}
    63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    64 
    46 
    65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
    47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
    48 
       
    49 \begin{frame}[c]
    66 \begin{frame}[c]
    50 \frametitle{Marks for CW6 (Part 1 + 2)}
    67 \frametitle{Preliminary 6}
    51 
    68 
    52 Raw marks (234 submissions):
    69 Raw marks (261 submissions):\bigskip
    53 
    70 
    54 \begin{itemize}
    71 \begin{itemize}
    55 \item 6\%: \hspace{4mm}163 students
    72 \item 3\%: \hspace{4mm}219
    56 \item 5\%: \hspace{4mm}29
    73 \item 2\%: \hspace{4mm}19
    57 \item 4\%: \hspace{4mm}3
       
    58 \item 3\%: \hspace{4mm}13
       
    59 \item 2\%: \hspace{4mm}3
       
    60 \item 1\%: \hspace{4mm}0
    74 \item 1\%: \hspace{4mm}0
    61 \item 0\%: \hspace{4mm}23 
    75 \item 0\%: \hspace{4mm}23 \;(4 no submission)
    62 \end{itemize}  
    76 \end{itemize}  
    63 \end{frame}
    77 \end{frame}
    64 
    78 
    65 
    79 
    66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    67 
       
    68 
       
    69 
    81 
    70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    71 \begin{frame}[c,fragile]
    83 \begin{frame}[c,fragile]
    72 
    84 
    73 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
    85 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
    84 \bigskip
    96 \bigskip
    85 
    97 
    86 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
    98 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
    87 def collatz(n: Long) : Long =
    99 def collatz(n: Long) : Long =
    88   collatzHelper(n, 0)
   100   collatzHelper(n, 0)
    89 \end{lstlisting}\pause
   101 \end{lstlisting}
    90 
       
    91 
   102 
    92 \end{frame}
   103 \end{frame}
    93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    94 
   105 
    95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    96 \begin{frame}[c,fragile]
   107 \begin{frame}[c,fragile]
       
   108 \frametitle{Default Arguments}
       
   109 
       
   110 \small
       
   111 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
       
   112 def collatzHelper(n: Int, a: Int = 0) : Int = ...
       
   113 
       
   114 
       
   115 collatzHelper(n, 3)
       
   116 collatzHelper(n, 0)
       
   117 
       
   118 collatzHelper(n)   // a = 0   
       
   119 \end{lstlisting}
       
   120 
       
   121 \DOWNarrow{1}{10.7}{3.4}
       
   122 
       
   123 \end{frame}
       
   124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       
   125 
       
   126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   127 \begin{frame}[c,fragile]
       
   128 \frametitle{Last Week: Options \& HO Funs.}
    97 
   129 
    98 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
   130 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
    99 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)}
   131 List(7,2,3,4,5,6).find(_ < 4)
   100 \end{lstlisting}\bigskip
   132 res: Option[Int] = Some(2)
   101 
   133  
   102 \tiny
   134 
       
   135 List(5,6,7,8,9).find(_ < 4)
       
   136 res: Option[Int] = None
       
   137 
       
   138 
       
   139 List(1,2,3,4,5).map(x => x * x)
       
   140 res: List[Int] = List(1, 4, 9, 16, 25)
       
   141 \end{lstlisting}
       
   142   
       
   143   \end{frame}
       
   144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   145  
       
   146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   147 \begin{frame}[c,fragile]
       
   148 \frametitle{Web-Crawler (1)}
       
   149 
       
   150 \small
       
   151 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-7mm]
       
   152 def get_page(url: String) : String = {
       
   153 Try(fromURL(url)("ISO-8859-1").take(10000).mkString)
       
   154    .getOrElse { println(s" Problem with: $url"); ""}
       
   155 }
       
   156 \end{lstlisting}
       
   157 \end{frame}
       
   158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   159  
       
   160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   161 \begin{frame}[c,fragile]
       
   162 \frametitle{Web-Crawler (2)}
       
   163 
       
   164 \small
       
   165 \begin{lstlisting}[language=Scala, numbers=none, 
       
   166                     xleftmargin=-7mm, escapeinside={(*@}{@*)}]
       
   167 val http_pattern = """(*@\textcolor{codegreen}{"}@*)https?://[\^(*@\textcolor{codegreen}{"}@*)]*(*@\textcolor{codegreen}{"}@*)""".r
       
   168 val email_pattern = 
       
   169  """([a-z\d\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
       
   170 
       
   171 
       
   172 def unquote(s: String) = s.drop(1).dropRight(1)
       
   173 
       
   174 
       
   175 def get_all_URLs(page: String): Set[String] = 
       
   176   http_pattern.findAllIn(page).map(unquote).toSet
       
   177 
       
   178   // returns all URLs in a page  
       
   179 \end{lstlisting}
       
   180   
       
   181   \end{frame}
       
   182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   183 
       
   184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   185 \begin{frame}[c,fragile]
       
   186 \frametitle{Web-Crawler (3)}
       
   187 
       
   188 \small
   103 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
   189 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
   104 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)}
   190 def crawl(url: String, n: Int) : Unit = {
   105 \end{lstlisting}\pause
   191   if (n == 0) ()
   106 
   192   else {
   107 
   193     println(s"  Visiting: $n $url")
   108 \end{frame}
   194     val page = get_page(url)
   109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   195     for (u <- get_all_URLs(page)) 
   110 
   196       crawl(u, n - 1)
   111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   197   }
   112 \begin{frame}[c,fragile]
   198 }
   113 
   199 \end{lstlisting}
   114 \small
   200 \end{frame}
   115 \begin{lstlisting}[language=Scala, xleftmargin=-4mm,numbers=left]
   201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   116  def process_ratings(lines: List[String]) = {
   202   
   117     val values = List[(String,String)]()
   203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   118 
   204 \begin{frame}[c,fragile]
   119     for(line <- lines) {
   205 \frametitle{Email Harvester}
   120         val splitList = ...
   206 
   121 
   207 \small
   122         if(splitList(2).toInt >= 4){
   208 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-3mm]
   123             val userID = splitList(0)
   209 def emails(url: String, n: Int) : Set[String] = {
   124             val movieID = splitList(1)
   210  if (n == 0) Set()
   125             val tuple = (userID, movieID)
   211  else {
   126             tuple :: values
   212   println(s"  Visiting: $n $url")
   127         }
   213   val page = get_page(url)
   128     }
   214   val new_emails = 
   129     
   215     email_pattern.findAllIn(page).toSet
   130     values
   216   new_emails ++ 
       
   217     (for (u <- get_all_URLs(page)) 
       
   218        yield emails(u, n - 1)).flatten
   131  }
   219  }
   132 \end{lstlisting}
   220 } 
   133 
   221 
   134 \normalsize
   222 \end{lstlisting}
   135 What does this function (always) return?
   223 \end{frame}
   136 
   224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   137 
   225   
   138 \end{frame}
   226 
   139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   227 
   140 
   228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   142 
       
   143 \begin{frame}[c]
   229 \begin{frame}[c]
   144 \frametitle{Jumping Towers}
   230 \frametitle{Jumping Towers}
   145 
   231 
   146 \begin{center}
   232 \begin{center}
   147 \begin{tikzpicture}[scale=1.2]
   233 \begin{tikzpicture}[scale=1.3]
   148   \draw[line width=1mm,cap=round] (0,0) -- (5,0);
   234   \draw[line width=1mm,cap=round] (0,0) -- (5,0);
   149   \draw[line width=1mm,cap=round] (0,1) -- (5,1);
   235   \draw[line width=1mm,cap=round] (0,1) -- (5,1);
   150 
   236 
   151   \draw[line width=1mm,cap=round] (0,0) -- (0,1);
   237   \draw[line width=1mm,cap=round] (0,0) -- (0,1);
   152   \node at (0.5,0.5) {\textbf{\Large 3}};
   238   \node at (0.5,0.5) {\textbf{\Large 3}};
   184 \end{frame}
   270 \end{frame}
   185 
   271 
   186 
   272 
   187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   188 
   274 
   189 
   275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   191 
       
   192 \begin{frame}[c]
   276 \begin{frame}[c]
   193 \frametitle{``Children'' / moves}
   277 \frametitle{``Children'' / moves}
   194 
   278 
   195 \begin{center}
   279 \begin{center}
   196   \begin{tikzpicture}
   280   \begin{tikzpicture}
   203      }
   287      }
   204      child {node {$[4,2,0,1]$\ldots}};
   288      child {node {$[4,2,0,1]$\ldots}};
   205 \end{tikzpicture}
   289 \end{tikzpicture}
   206 \end{center}
   290 \end{center}
   207 
   291 
   208 
   292 \end{frame}
   209 
   293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   210 \end{frame}
   294 
   211 
   295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
   212 
   296 \begin{frame}[c,fragile]
   213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   297 \frametitle{Reverse Polish Notation}
   214 
   298 
   215 
   299 {\Large\bl{$(3 + 1) * (2 + 9)$}}\bigskip
       
   300 
       
   301 {\Large$\Rightarrow$}\bigskip
       
   302 
       
   303 {\;\;\Large\bl{$3\;\;1\;+\;2\;\;9\;+\;*$}}
       
   304 \end{frame}
       
   305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   306  
       
   307 
       
   308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
       
   309 \begin{frame}[c,fragile]
       
   310 \frametitle{Sudoku}
       
   311 
       
   312 A very simple-minded version on 110 problems:\bigskip
       
   313 
       
   314 \begin{itemize}
       
   315 \item 1 core: 800 secs
       
   316 \item 2 cores: 400 secs
       
   317 \item 8 cores: 290 secs
       
   318 \item 18 cores: 142 secs
       
   319 \end{itemize}
       
   320 
       
   321 \end{frame}
       
   322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   323  
   216 
   324 
   217 \end{document}
   325 \end{document}
   218 
   326 
   219 
   327 
   220 \end{document}
   328 \end{document}