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