51
|
1 |
// Scala Lecture 2
|
|
2 |
//=================
|
|
3 |
|
204
|
4 |
// UNFINISHED BUSINESS from Lecture 1
|
|
5 |
//====================================
|
|
6 |
|
|
7 |
|
|
8 |
// for measuring time
|
|
9 |
def time_needed[T](n: Int, code: => T) = {
|
|
10 |
val start = System.nanoTime()
|
|
11 |
for (i <- (0 to n)) code
|
|
12 |
val end = System.nanoTime()
|
|
13 |
(end - start) / 1.0e9
|
|
14 |
}
|
|
15 |
|
|
16 |
|
|
17 |
val list = (1 to 1000000).toList
|
|
18 |
time_needed(10, for (n <- list) yield n + 42)
|
|
19 |
time_needed(10, for (n <- list.par) yield n + 42)
|
|
20 |
|
212
|
21 |
// (ONLY WORKS OUT-OF-THE-BOX IN SCALA 2.11.8, not in SCALA 2.12)
|
|
22 |
// (would need to have this wrapped into a function, or
|
|
23 |
// REPL called with scala -Yrepl-class-based)
|
204
|
24 |
|
212
|
25 |
|
|
26 |
// Just for Fun: Mutable vs Immutable
|
|
27 |
//====================================
|
204
|
28 |
//
|
|
29 |
// - no vars, no ++i, no +=
|
|
30 |
// - no mutable data-structures (no Arrays, no ListBuffers)
|
|
31 |
|
|
32 |
|
212
|
33 |
// Q: Count how many elements are in the intersections of
|
|
34 |
// two sets?
|
204
|
35 |
|
|
36 |
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
|
|
37 |
var count = 0
|
|
38 |
for (x <- A; if B contains x) count += 1
|
|
39 |
count
|
|
40 |
}
|
|
41 |
|
|
42 |
val A = (1 to 1000).toSet
|
|
43 |
val B = (1 to 1000 by 4).toSet
|
|
44 |
|
|
45 |
count_intersection(A, B)
|
|
46 |
|
|
47 |
// but do not try to add .par to the for-loop above
|
|
48 |
|
|
49 |
|
|
50 |
//propper parallel version
|
|
51 |
def count_intersection2(A: Set[Int], B: Set[Int]) : Int =
|
|
52 |
A.par.count(x => B contains x)
|
|
53 |
|
|
54 |
count_intersection2(A, B)
|
|
55 |
|
|
56 |
|
|
57 |
val A = (1 to 1000000).toSet
|
|
58 |
val B = (1 to 1000000 by 4).toSet
|
|
59 |
|
|
60 |
time_needed(100, count_intersection(A, B))
|
|
61 |
time_needed(100, count_intersection2(A, B))
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
// For-Comprehensions Again
|
|
66 |
//==========================
|
|
67 |
|
|
68 |
// the first produces a result, while the second does not
|
|
69 |
for (n <- List(1, 2, 3, 4, 5)) yield n * n
|
|
70 |
|
|
71 |
|
|
72 |
for (n <- List(1, 2, 3, 4, 5)) println(n)
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
// Higher-Order Functions
|
|
77 |
//========================
|
|
78 |
|
|
79 |
// functions can take functions as arguments
|
|
80 |
|
|
81 |
def even(x: Int) : Boolean = x % 2 == 0
|
|
82 |
def odd(x: Int) : Boolean = x % 2 == 1
|
|
83 |
|
|
84 |
val lst = (1 to 10).toList
|
|
85 |
|
|
86 |
lst.filter(x => even(x))
|
|
87 |
lst.filter(even(_))
|
|
88 |
lst.filter(even)
|
|
89 |
|
|
90 |
lst.count(even)
|
|
91 |
|
212
|
92 |
|
|
93 |
lst.find(even)
|
|
94 |
|
|
95 |
val ps = List((3, 0), (3, 2), (4, 2), (2, 2), (2, 0), (1, 1), (1, 0))
|
204
|
96 |
|
212
|
97 |
lst.sortWith(_ > _)
|
|
98 |
lst.sortWith(_ < _)
|
204
|
99 |
|
212
|
100 |
def lex(x: (Int, Int), y: (Int, Int)) : Boolean =
|
|
101 |
if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
|
|
102 |
|
|
103 |
ps.sortWith(lex)
|
204
|
104 |
|
|
105 |
ps.sortBy(_._1)
|
|
106 |
ps.sortBy(_._2)
|
|
107 |
|
|
108 |
ps.maxBy(_._1)
|
|
109 |
ps.maxBy(_._2)
|
|
110 |
|
|
111 |
|
|
112 |
|
212
|
113 |
// maps (lower-case)
|
|
114 |
//===================
|
204
|
115 |
|
212
|
116 |
def double(x: Int): Int = x + x
|
204
|
117 |
def square(x: Int): Int = x * x
|
|
118 |
|
212
|
119 |
|
|
120 |
|
204
|
121 |
val lst = (1 to 10).toList
|
|
122 |
|
212
|
123 |
lst.map(x => (double(x), square(x)))
|
|
124 |
|
204
|
125 |
lst.map(square)
|
|
126 |
|
|
127 |
// this is actually what for is defined at in Scala
|
|
128 |
|
|
129 |
lst.map(n => square(n))
|
|
130 |
for (n <- lst) yield square(n)
|
|
131 |
|
|
132 |
// this can be iterated
|
|
133 |
|
|
134 |
lst.map(square).filter(_ > 4)
|
|
135 |
|
|
136 |
lst.map(square).filter(_ > 4).map(square)
|
|
137 |
|
|
138 |
|
|
139 |
// lets define our own functions
|
|
140 |
// type of functions, for example f: Int => Int
|
|
141 |
|
212
|
142 |
lst.tail
|
|
143 |
|
204
|
144 |
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
|
|
145 |
if (lst == Nil) Nil
|
|
146 |
else f(lst.head) :: my_map_int(lst.tail, f)
|
|
147 |
}
|
|
148 |
|
|
149 |
my_map_int(lst, square)
|
|
150 |
|
|
151 |
|
|
152 |
// same function using pattern matching: a kind
|
|
153 |
// of switch statement on steroids (see more later on)
|
|
154 |
|
|
155 |
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = lst match {
|
|
156 |
case Nil => Nil
|
|
157 |
case x::xs => f(x)::my_map_int(xs, f)
|
|
158 |
}
|
|
159 |
|
|
160 |
|
|
161 |
// other function types
|
|
162 |
//
|
|
163 |
// f1: (Int, Int) => Int
|
|
164 |
// f2: List[String] => Option[Int]
|
|
165 |
// ...
|
212
|
166 |
val lst = (1 to 10).toList
|
204
|
167 |
|
|
168 |
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
|
|
169 |
case Nil => 0
|
|
170 |
case x::xs => f(x) + sumOf(f, xs)
|
|
171 |
}
|
|
172 |
|
|
173 |
def sum_squares(lst: List[Int]) = sumOf(square, lst)
|
|
174 |
def sum_cubes(lst: List[Int]) = sumOf(x => x * x * x, lst)
|
|
175 |
|
|
176 |
sum_squares(lst)
|
|
177 |
sum_cubes(lst)
|
|
178 |
|
|
179 |
// lets try it factorial
|
212
|
180 |
def fact(n: Int) : Int =
|
|
181 |
if (n == 0) 1 else n * fact(n - 1)
|
204
|
182 |
|
|
183 |
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
|
|
184 |
sum_fact(lst)
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
212
|
190 |
// Map type (upper-case)
|
|
191 |
//=======================
|
204
|
192 |
|
|
193 |
// Note the difference between map and Map
|
|
194 |
|
|
195 |
def factors(n: Int) : List[Int] =
|
|
196 |
((1 until n).filter { divisor =>
|
|
197 |
n % divisor == 0
|
|
198 |
}).toList
|
|
199 |
|
|
200 |
|
|
201 |
var ls = (1 to 10).toList
|
|
202 |
|
|
203 |
val facs = ls.map(n => (n, factors(n)))
|
|
204 |
|
|
205 |
facs.find(_._1 == 4)
|
|
206 |
|
|
207 |
// works for lists of pairs
|
|
208 |
facs.toMap
|
|
209 |
|
|
210 |
|
|
211 |
facs.toMap.get(4)
|
212
|
212 |
facs.toMap.getOrElse(42, Nil)
|
204
|
213 |
|
|
214 |
val facsMap = facs.toMap
|
|
215 |
|
|
216 |
val facsMap0 = facsMap + (0 -> List(1,2,3,4,5))
|
212
|
217 |
facsMap0.get(1)
|
204
|
218 |
|
|
219 |
val facsMap4 = facsMap + (1 -> List(1,2,3,4,5))
|
|
220 |
facsMap.get(1)
|
|
221 |
facsMap4.get(1)
|
|
222 |
|
|
223 |
val ls = List("one", "two", "three", "four", "five")
|
|
224 |
ls.groupBy(_.length)
|
|
225 |
|
212
|
226 |
ls.groupBy(_.length).get(2)
|
204
|
227 |
|
|
228 |
|
51
|
229 |
|
|
230 |
// Option type
|
|
231 |
//=============
|
53
|
232 |
|
192
|
233 |
//in Java if something unusually happens, you return null;
|
204
|
234 |
//
|
53
|
235 |
//in Scala you use Option
|
|
236 |
// - if the value is present, you use Some(value)
|
|
237 |
// - if no value is present, you use None
|
|
238 |
|
|
239 |
|
192
|
240 |
List(7,2,3,4,5,6).find(_ < 4)
|
53
|
241 |
List(5,6,7,8,9).find(_ < 4)
|
|
242 |
|
204
|
243 |
// operations on options
|
58
|
244 |
|
51
|
245 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
|
246 |
|
|
247 |
lst.flatten
|
53
|
248 |
|
192
|
249 |
Some(1).get
|
212
|
250 |
None.get
|
51
|
251 |
|
53
|
252 |
Some(1).isDefined
|
|
253 |
None.isDefined
|
|
254 |
|
212
|
255 |
|
|
256 |
None.isDefined
|
|
257 |
|
51
|
258 |
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
|
|
259 |
|
|
260 |
for ((x, y) <- ps) yield {
|
|
261 |
if (y == 0) None else Some(x / y)
|
|
262 |
}
|
|
263 |
|
192
|
264 |
// getOrElse is for setting a default value
|
53
|
265 |
|
|
266 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
204
|
267 |
|
57
|
268 |
for (x <- lst) yield x.getOrElse(0)
|
|
269 |
|
|
270 |
|
53
|
271 |
|
|
272 |
|
192
|
273 |
// error handling with Option (no exceptions)
|
57
|
274 |
//
|
|
275 |
// Try(something).getOrElse(what_to_do_in_an_exception)
|
|
276 |
//
|
53
|
277 |
import scala.util._
|
|
278 |
import io.Source
|
|
279 |
|
212
|
280 |
|
|
281 |
Source.fromURL("""http://www.inf.ucl.ac.uk/staff/urbanc/""").mkString
|
53
|
282 |
|
192
|
283 |
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
|
53
|
284 |
|
192
|
285 |
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
|
53
|
286 |
|
|
287 |
|
204
|
288 |
// a function that turns strings into numbers (similar to .toInt)
|
212
|
289 |
Integer.parseInt("12u34")
|
204
|
290 |
|
|
291 |
|
|
292 |
def get_me_an_int(s: String) : Option[Int] =
|
53
|
293 |
Try(Some(Integer.parseInt(s))).getOrElse(None)
|
|
294 |
|
204
|
295 |
val lst = List("12345", "foo", "5432", "bar", "x21", "456")
|
53
|
296 |
for (x <- lst) yield get_me_an_int(x)
|
|
297 |
|
|
298 |
// summing all the numbers
|
204
|
299 |
|
212
|
300 |
lst.map(get_me_an_int).flatten.sum
|
204
|
301 |
lst.map(get_me_an_int).flatten.sum
|
|
302 |
|
|
303 |
|
212
|
304 |
lst.flatMap(get_me_an_int).map(_.toString)
|
53
|
305 |
|
|
306 |
|
|
307 |
// This may not look any better than working with null in Java, but to
|
|
308 |
// see the value, you have to put yourself in the shoes of the
|
|
309 |
// consumer of the get_me_an_int function, and imagine you didn't
|
|
310 |
// write that function.
|
|
311 |
//
|
|
312 |
// In Java, if you didn't write this function, you'd have to depend on
|
192
|
313 |
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc,
|
57
|
314 |
// you might not know that get_me_an_int could return a null, and your
|
|
315 |
// code could potentially throw a NullPointerException.
|
53
|
316 |
|
|
317 |
|
192
|
318 |
|
58
|
319 |
// even Scala is not immune to problems like this:
|
|
320 |
|
192
|
321 |
List(5,6,7,8,9).indexOf(7)
|
204
|
322 |
List(5,6,7,8,9).indexOf(10)
|
212
|
323 |
List(5,6,7,8,9)(-1)
|
192
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
// Pattern Matching
|
|
328 |
//==================
|
|
329 |
|
|
330 |
// A powerful tool which is supposed to come to Java in a few years
|
|
331 |
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
|
|
332 |
// has it for many years ;o)
|
|
333 |
|
|
334 |
// The general schema:
|
|
335 |
//
|
|
336 |
// expression match {
|
|
337 |
// case pattern1 => expression1
|
|
338 |
// case pattern2 => expression2
|
|
339 |
// ...
|
|
340 |
// case patternN => expressionN
|
|
341 |
// }
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
204
|
346 |
// remember?
|
192
|
347 |
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
|
|
348 |
|
|
349 |
|
212
|
350 |
def my_flatten(xs: List[Option[Int]]): List[Int] = xs match {
|
|
351 |
case Nil => Nil
|
|
352 |
case None::rest => my_flatten(rest)
|
|
353 |
case Some(v)::foo => {
|
|
354 |
v :: my_flatten(foo)
|
|
355 |
}
|
192
|
356 |
}
|
58
|
357 |
|
|
358 |
|
192
|
359 |
// another example
|
|
360 |
def get_me_a_string(n: Int): String = n match {
|
212
|
361 |
case 0 | 1 | 2 => "small"
|
|
362 |
case _ => "big"
|
192
|
363 |
}
|
|
364 |
|
|
365 |
get_me_a_string(0)
|
|
366 |
|
212
|
367 |
|
192
|
368 |
// you can also have cases combined
|
|
369 |
def season(month: String) = month match {
|
|
370 |
case "March" | "April" | "May" => "It's spring"
|
|
371 |
case "June" | "July" | "August" => "It's summer"
|
|
372 |
case "September" | "October" | "November" => "It's autumn"
|
204
|
373 |
case "December" => "It's winter"
|
|
374 |
case "January" | "February" => "It's unfortunately winter"
|
192
|
375 |
}
|
|
376 |
|
|
377 |
println(season("November"))
|
|
378 |
|
|
379 |
// What happens if no case matches?
|
212
|
380 |
println(season("foobar"))
|
192
|
381 |
|
|
382 |
|
204
|
383 |
// Silly: fizz buzz
|
192
|
384 |
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
|
|
385 |
case (0, 0) => "fizz buzz"
|
|
386 |
case (0, _) => "fizz"
|
|
387 |
case (_, 0) => "buzz"
|
|
388 |
case _ => n.toString
|
|
389 |
}
|
|
390 |
|
|
391 |
for (n <- 0 to 20)
|
|
392 |
println(fizz_buzz(n))
|
|
393 |
|
|
394 |
|
|
395 |
// User-defined Datatypes
|
|
396 |
//========================
|
|
397 |
|
|
398 |
|
204
|
399 |
abstract class Colour
|
|
400 |
case object Red extends Colour
|
|
401 |
case object Green extends Colour
|
|
402 |
case object Blue extends Colour
|
192
|
403 |
|
204
|
404 |
def fav_colour(c: Colour) : Boolean = c match {
|
|
405 |
case Red => false
|
|
406 |
case Green => true
|
|
407 |
case Blue => false
|
173
|
408 |
}
|
|
409 |
|
204
|
410 |
fav_colour(Green)
|
|
411 |
|
192
|
412 |
|
204
|
413 |
// ... a bit more useful: Roman Numerals
|
|
414 |
|
|
415 |
abstract class RomanDigit
|
|
416 |
case object I extends RomanDigit
|
|
417 |
case object V extends RomanDigit
|
|
418 |
case object X extends RomanDigit
|
|
419 |
case object L extends RomanDigit
|
|
420 |
case object C extends RomanDigit
|
|
421 |
case object D extends RomanDigit
|
|
422 |
case object M extends RomanDigit
|
|
423 |
|
|
424 |
type RomanNumeral = List[RomanDigit]
|
192
|
425 |
|
212
|
426 |
List(X,I)
|
|
427 |
|
|
428 |
I -> 1
|
|
429 |
II -> 2
|
|
430 |
III -> 3
|
|
431 |
IV -> 4
|
|
432 |
V -> 5
|
|
433 |
VI -> 6
|
|
434 |
VII -> 7
|
|
435 |
VIII -> 8
|
|
436 |
IX -> 9
|
|
437 |
X -> X
|
|
438 |
|
204
|
439 |
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match {
|
|
440 |
case Nil => 0
|
|
441 |
case M::r => 1000 + RomanNumeral2Int(r)
|
|
442 |
case C::M::r => 900 + RomanNumeral2Int(r)
|
|
443 |
case D::r => 500 + RomanNumeral2Int(r)
|
|
444 |
case C::D::r => 400 + RomanNumeral2Int(r)
|
|
445 |
case C::r => 100 + RomanNumeral2Int(r)
|
|
446 |
case X::C::r => 90 + RomanNumeral2Int(r)
|
|
447 |
case L::r => 50 + RomanNumeral2Int(r)
|
|
448 |
case X::L::r => 40 + RomanNumeral2Int(r)
|
|
449 |
case X::r => 10 + RomanNumeral2Int(r)
|
|
450 |
case I::X::r => 9 + RomanNumeral2Int(r)
|
|
451 |
case V::r => 5 + RomanNumeral2Int(r)
|
|
452 |
case I::V::r => 4 + RomanNumeral2Int(r)
|
|
453 |
case I::r => 1 + RomanNumeral2Int(r)
|
192
|
454 |
}
|
|
455 |
|
204
|
456 |
RomanNumeral2Int(List(I,V)) // 4
|
|
457 |
RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid Roman number)
|
|
458 |
RomanNumeral2Int(List(V,I)) // 6
|
|
459 |
RomanNumeral2Int(List(I,X)) // 9
|
|
460 |
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
|
|
461 |
RomanNumeral2Int(List(M,M,X,V,I,I)) // 2017
|
|
462 |
|
192
|
463 |
|
204
|
464 |
// another example
|
|
465 |
//=================
|
192
|
466 |
|
212
|
467 |
// Once upon a time, in a complete fictional
|
|
468 |
// country there were Persons...
|
192
|
469 |
|
|
470 |
|
|
471 |
abstract class Person
|
204
|
472 |
case object King extends Person
|
192
|
473 |
case class Peer(deg: String, terr: String, succ: Int) extends Person
|
|
474 |
case class Knight(name: String) extends Person
|
|
475 |
case class Peasant(name: String) extends Person
|
212
|
476 |
|
173
|
477 |
|
192
|
478 |
def title(p: Person): String = p match {
|
204
|
479 |
case King => "His Majesty the King"
|
192
|
480 |
case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
|
|
481 |
case Knight(name) => s"Sir ${name}"
|
|
482 |
case Peasant(name) => name
|
|
483 |
}
|
173
|
484 |
|
192
|
485 |
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
|
204
|
486 |
case (King, _) => true
|
192
|
487 |
case (Peer(_,_,_), Knight(_)) => true
|
|
488 |
case (Peer(_,_,_), Peasant(_)) => true
|
204
|
489 |
case (Peer(_,_,_), Clown) => true
|
192
|
490 |
case (Knight(_), Peasant(_)) => true
|
204
|
491 |
case (Knight(_), Clown) => true
|
|
492 |
case (Clown, Peasant(_)) => true
|
192
|
493 |
case _ => false
|
|
494 |
}
|
|
495 |
|
|
496 |
val people = List(Knight("David"),
|
|
497 |
Peer("Duke", "Norfolk", 84),
|
|
498 |
Peasant("Christian"),
|
204
|
499 |
King,
|
|
500 |
Clown)
|
192
|
501 |
|
212
|
502 |
println(people.sortWith(superior).mkString("\n"))
|
|
503 |
|
|
504 |
print("123\\n456")
|
192
|
505 |
|
173
|
506 |
|
204
|
507 |
// Tail recursion
|
|
508 |
//================
|
147
|
509 |
|
|
510 |
|
204
|
511 |
def fact(n: Long): Long =
|
|
512 |
if (n == 0) 1 else n * fact(n - 1)
|
147
|
513 |
|
204
|
514 |
fact(10) //ok
|
|
515 |
fact(10000) // produces a stackoverflow
|
147
|
516 |
|
204
|
517 |
def factT(n: BigInt, acc: BigInt): BigInt =
|
|
518 |
if (n == 0) acc else factT(n - 1, n * acc)
|
147
|
519 |
|
204
|
520 |
factT(10, 1)
|
|
521 |
factT(100000, 1)
|
192
|
522 |
|
204
|
523 |
// there is a flag for ensuring a function is tail recursive
|
|
524 |
import scala.annotation.tailrec
|
167
|
525 |
|
204
|
526 |
@tailrec
|
|
527 |
def factT(n: BigInt, acc: BigInt): BigInt =
|
|
528 |
if (n == 0) acc else factT(n - 1, n * acc)
|
167
|
529 |
|
|
530 |
|
|
531 |
|
204
|
532 |
// for tail-recursive functions the Scala compiler
|
|
533 |
// generates loop-like code, which does not need
|
|
534 |
// to allocate stack-space in each recursive
|
|
535 |
// call; Scala can do this only for tail-recursive
|
|
536 |
// functions
|
|
537 |
|
147
|
538 |
|
212
|
539 |
// A Web Crawler / Email Harvester
|
|
540 |
//=================================
|
204
|
541 |
//
|
212
|
542 |
// the idea is to look for links using the
|
|
543 |
// regular expression "https?://[^"]*" and for
|
|
544 |
// email addresses using another regex.
|
204
|
545 |
|
|
546 |
import io.Source
|
|
547 |
import scala.util._
|
|
548 |
|
|
549 |
// gets the first 10K of a web-page
|
|
550 |
def get_page(url: String) : String = {
|
|
551 |
Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
|
|
552 |
getOrElse { println(s" Problem with: $url"); ""}
|
147
|
553 |
}
|
|
554 |
|
204
|
555 |
// regex for URLs and emails
|
|
556 |
val http_pattern = """"https?://[^"]*"""".r
|
|
557 |
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
|
|
558 |
|
212
|
559 |
//email_pattern.findAllIn
|
|
560 |
// ("foo bla christian@kcl.ac.uk 1234567").toList
|
|
561 |
|
204
|
562 |
|
|
563 |
// drops the first and last character from a string
|
|
564 |
def unquote(s: String) = s.drop(1).dropRight(1)
|
|
565 |
|
|
566 |
def get_all_URLs(page: String): Set[String] =
|
|
567 |
http_pattern.findAllIn(page).map(unquote).toSet
|
|
568 |
|
|
569 |
// naive version of crawl - searches until a given depth,
|
|
570 |
// visits pages potentially more than once
|
|
571 |
def crawl(url: String, n: Int) : Set[String] = {
|
|
572 |
if (n == 0) Set()
|
|
573 |
else {
|
|
574 |
println(s" Visiting: $n $url")
|
|
575 |
val page = get_page(url)
|
|
576 |
val new_emails = email_pattern.findAllIn(page).toSet
|
212
|
577 |
new_emails ++ (for (u <- get_all_URLs(page)) yield crawl(u, n - 1)).flatten
|
204
|
578 |
}
|
147
|
579 |
}
|
|
580 |
|
204
|
581 |
// some starting URLs for the crawler
|
|
582 |
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
|
147
|
583 |
|
204
|
584 |
crawl(startURL, 2)
|
|
585 |
|
|
586 |
|
|
587 |
|
|
588 |
|
150
|
589 |
|
|
590 |
|
|
591 |
|
192
|
592 |
// Sudoku
|
|
593 |
//========
|
53
|
594 |
|
57
|
595 |
// THE POINT OF THIS CODE IS NOT TO BE SUPER
|
|
596 |
// EFFICIENT AND FAST, just explaining exhaustive
|
|
597 |
// depth-first search
|
|
598 |
|
|
599 |
|
55
|
600 |
val game0 = """.14.6.3..
|
|
601 |
|62...4..9
|
|
602 |
|.8..5.6..
|
|
603 |
|.6.2....3
|
|
604 |
|.7..1..5.
|
|
605 |
|5....9.6.
|
|
606 |
|..6.2..3.
|
|
607 |
|1..5...92
|
|
608 |
|..7.9.41.""".stripMargin.replaceAll("\\n", "")
|
|
609 |
|
|
610 |
type Pos = (Int, Int)
|
|
611 |
val EmptyValue = '.'
|
|
612 |
val MaxValue = 9
|
|
613 |
|
|
614 |
val allValues = "123456789".toList
|
|
615 |
val indexes = (0 to 8).toList
|
|
616 |
|
57
|
617 |
|
|
618 |
|
55
|
619 |
|
192
|
620 |
def empty(game: String) = game.indexOf(EmptyValue)
|
|
621 |
def isDone(game: String) = empty(game) == -1
|
|
622 |
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
|
57
|
623 |
|
192
|
624 |
|
|
625 |
def get_row(game: String, y: Int) = indexes.map(col => game(y * MaxValue + col))
|
|
626 |
def get_col(game: String, x: Int) = indexes.map(row => game(x + row * MaxValue))
|
147
|
627 |
|
57
|
628 |
def get_box(game: String, pos: Pos): List[Char] = {
|
55
|
629 |
def base(p: Int): Int = (p / 3) * 3
|
|
630 |
val x0 = base(pos._1)
|
|
631 |
val y0 = base(pos._2)
|
|
632 |
val ys = (y0 until y0 + 3).toList
|
|
633 |
(x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
|
|
634 |
}
|
|
635 |
|
|
636 |
|
192
|
637 |
//get_row(game0, 0)
|
|
638 |
//get_row(game0, 1)
|
|
639 |
//get_box(game0, (3,1))
|
|
640 |
|
|
641 |
def update(game: String, pos: Int, value: Char): String = game.updated(pos, value)
|
55
|
642 |
|
|
643 |
def toAvoid(game: String, pos: Pos): List[Char] =
|
57
|
644 |
(get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
|
55
|
645 |
|
192
|
646 |
def candidates(game: String, pos: Pos): List[Char] = allValues diff toAvoid(game,pos)
|
55
|
647 |
|
|
648 |
//candidates(game0, (0,0))
|
|
649 |
|
192
|
650 |
def pretty(game: String): String = "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
|
55
|
651 |
|
|
652 |
def search(game: String): List[String] = {
|
|
653 |
if (isDone(game)) List(game)
|
192
|
654 |
else
|
|
655 |
candidates(game, emptyPosition(game)).map(c => search(update(game, empty(game), c))).toList.flatten
|
55
|
656 |
}
|
|
657 |
|
|
658 |
|
|
659 |
val game1 = """23.915...
|
|
660 |
|...2..54.
|
|
661 |
|6.7......
|
|
662 |
|..1.....9
|
|
663 |
|89.5.3.17
|
|
664 |
|5.....6..
|
|
665 |
|......9.5
|
|
666 |
|.16..7...
|
|
667 |
|...329..1""".stripMargin.replaceAll("\\n", "")
|
|
668 |
|
57
|
669 |
|
192
|
670 |
// game that is in the hard category
|
55
|
671 |
val game2 = """8........
|
|
672 |
|..36.....
|
|
673 |
|.7..9.2..
|
|
674 |
|.5...7...
|
|
675 |
|....457..
|
|
676 |
|...1...3.
|
|
677 |
|..1....68
|
|
678 |
|..85...1.
|
|
679 |
|.9....4..""".stripMargin.replaceAll("\\n", "")
|
|
680 |
|
|
681 |
// game with multiple solutions
|
|
682 |
val game3 = """.8...9743
|
|
683 |
|.5...8.1.
|
|
684 |
|.1.......
|
|
685 |
|8....5...
|
|
686 |
|...8.4...
|
|
687 |
|...3....6
|
|
688 |
|.......7.
|
|
689 |
|.3.5...8.
|
|
690 |
|9724...5.""".stripMargin.replaceAll("\\n", "")
|
|
691 |
|
57
|
692 |
|
192
|
693 |
search(game0).map(pretty)
|
|
694 |
search(game1).map(pretty)
|
55
|
695 |
|
|
696 |
// for measuring time
|
|
697 |
def time_needed[T](i: Int, code: => T) = {
|
|
698 |
val start = System.nanoTime()
|
|
699 |
for (j <- 1 to i) code
|
|
700 |
val end = System.nanoTime()
|
|
701 |
((end - start) / i / 1.0e9) + " secs"
|
|
702 |
}
|
|
703 |
|
|
704 |
search(game2).map(pretty)
|
57
|
705 |
search(game3).distinct.length
|
192
|
706 |
time_needed(3, search(game2))
|
|
707 |
time_needed(3, search(game3))
|
55
|
708 |
|
53
|
709 |
|
|
710 |
|
|
711 |
|
192
|
712 |
|