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}}; |