| author | Christian Urban <urbanc@in.tum.de> | 
| Tue, 12 Nov 2019 00:41:00 +0000 | |
| changeset 318 | f1215a72cd88 | 
| parent 223 | 417df5986615 | 
| child 320 | 90aed247c8cf | 
| permissions | -rw-r--r-- | 
| 67 | 1 | // Scala Lecture 3 | 
| 2 | //================= | |
| 3 | ||
| 217 | 4 | |
| 5 | // A Web Crawler / Email Harvester | |
| 6 | //================================= | |
| 7 | // | |
| 8 | // the idea is to look for links using the | |
| 9 | // regular expression "https?://[^"]*" and for | |
| 218 | 10 | // email addresses using yet another regex. | 
| 217 | 11 | |
| 12 | import io.Source | |
| 13 | import scala.util._ | |
| 155 | 14 | |
| 217 | 15 | // gets the first 10K of a web-page | 
| 16 | def get_page(url: String) : String = {
 | |
| 17 |   Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
 | |
| 18 |     getOrElse { println(s"  Problem with: $url"); ""}
 | |
| 19 | } | |
| 155 | 20 | |
| 217 | 21 | // regex for URLs and emails | 
| 22 | val http_pattern = """"https?://[^"]*"""".r | |
| 23 | val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
 | |
| 24 | ||
| 218 | 25 | // val s = "foo bla christian@kcl.ac.uk 1234567" | 
| 26 | // email_pattern.findAllIn(s).toList | |
| 155 | 27 | |
| 217 | 28 | // drops the first and last character from a string | 
| 29 | def unquote(s: String) = s.drop(1).dropRight(1) | |
| 155 | 30 | |
| 217 | 31 | def get_all_URLs(page: String): Set[String] = | 
| 32 | http_pattern.findAllIn(page).map(unquote).toSet | |
| 155 | 33 | |
| 217 | 34 | // naive version of crawl - searches until a given depth, | 
| 35 | // visits pages potentially more than once | |
| 218 | 36 | |
| 217 | 37 | def crawl(url: String, n: Int) : Set[String] = {
 | 
| 38 | if (n == 0) Set() | |
| 39 |   else {
 | |
| 40 | println(s" Visiting: $n $url") | |
| 41 | val page = get_page(url) | |
| 42 | val new_emails = email_pattern.findAllIn(page).toSet | |
| 43 | new_emails ++ | |
| 218 | 44 | (for (u <- get_all_URLs(page).par) yield crawl(u, n - 1)).flatten | 
| 217 | 45 | } | 
| 155 | 46 | } | 
| 47 | ||
| 217 | 48 | // some starting URLs for the crawler | 
| 49 | val startURL = """https://nms.kcl.ac.uk/christian.urban/""" | |
| 50 | crawl(startURL, 2) | |
| 51 | ||
| 155 | 52 | |
| 318 | 53 | // User-defined Datatypes | 
| 54 | //======================== | |
| 55 | ||
| 56 | ||
| 57 | abstract class Colour | |
| 58 | case object Red extends Colour | |
| 59 | case object Green extends Colour | |
| 60 | case object Blue extends Colour | |
| 61 | ||
| 62 | def fav_colour(c: Colour) : Boolean = c match {
 | |
| 63 | case Red => false | |
| 64 | case Green => true | |
| 65 | case Blue => false | |
| 66 | } | |
| 67 | ||
| 68 | fav_colour(Green) | |
| 69 | ||
| 70 | ||
| 71 | // ... a tiny bit more useful: Roman Numerals | |
| 72 | ||
| 73 | abstract class RomanDigit | |
| 74 | case object I extends RomanDigit | |
| 75 | case object V extends RomanDigit | |
| 76 | case object X extends RomanDigit | |
| 77 | case object L extends RomanDigit | |
| 78 | case object C extends RomanDigit | |
| 79 | case object D extends RomanDigit | |
| 80 | case object M extends RomanDigit | |
| 81 | ||
| 82 | type RomanNumeral = List[RomanDigit] | |
| 83 | ||
| 84 | List(X,I) | |
| 85 | ||
| 86 | /* | |
| 87 | I -> 1 | |
| 88 | II -> 2 | |
| 89 | III -> 3 | |
| 90 | IV -> 4 | |
| 91 | V -> 5 | |
| 92 | VI -> 6 | |
| 93 | VII -> 7 | |
| 94 | VIII -> 8 | |
| 95 | IX -> 9 | |
| 96 | X -> X | |
| 97 | */ | |
| 98 | ||
| 99 | def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
 | |
| 100 | case Nil => 0 | |
| 101 | case M::r => 1000 + RomanNumeral2Int(r) | |
| 102 | case C::M::r => 900 + RomanNumeral2Int(r) | |
| 103 | case D::r => 500 + RomanNumeral2Int(r) | |
| 104 | case C::D::r => 400 + RomanNumeral2Int(r) | |
| 105 | case C::r => 100 + RomanNumeral2Int(r) | |
| 106 | case X::C::r => 90 + RomanNumeral2Int(r) | |
| 107 | case L::r => 50 + RomanNumeral2Int(r) | |
| 108 | case X::L::r => 40 + RomanNumeral2Int(r) | |
| 109 | case X::r => 10 + RomanNumeral2Int(r) | |
| 110 | case I::X::r => 9 + RomanNumeral2Int(r) | |
| 111 | case V::r => 5 + RomanNumeral2Int(r) | |
| 112 | case I::V::r => 4 + RomanNumeral2Int(r) | |
| 113 | case I::r => 1 + RomanNumeral2Int(r) | |
| 114 | } | |
| 115 | ||
| 116 | RomanNumeral2Int(List(I,V)) // 4 | |
| 117 | RomanNumeral2Int(List(I,I,I,I)) // 4 (invalid Roman number) | |
| 118 | RomanNumeral2Int(List(V,I)) // 6 | |
| 119 | RomanNumeral2Int(List(I,X)) // 9 | |
| 120 | RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979 | |
| 121 | RomanNumeral2Int(List(M,M,X,V,I,I)) // 2017 | |
| 122 | ||
| 123 | ||
| 124 | // another example | |
| 125 | //================= | |
| 126 | ||
| 127 | // Once upon a time, in a complete fictional | |
| 128 | // country there were Persons... | |
| 129 | ||
| 130 | ||
| 131 | abstract class Person | |
| 132 | case object King extends Person | |
| 133 | case class Peer(deg: String, terr: String, succ: Int) extends Person | |
| 134 | case class Knight(name: String) extends Person | |
| 135 | case class Peasant(name: String) extends Person | |
| 136 | ||
| 137 | ||
| 138 | def title(p: Person): String = p match {
 | |
| 139 | case King => "His Majesty the King" | |
| 140 |   case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
 | |
| 141 |   case Knight(name) => s"Sir ${name}"
 | |
| 142 | case Peasant(name) => name | |
| 143 | } | |
| 144 | ||
| 145 | def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
 | |
| 146 | case (King, _) => true | |
| 147 | case (Peer(_,_,_), Knight(_)) => true | |
| 148 | case (Peer(_,_,_), Peasant(_)) => true | |
| 149 | case (Peer(_,_,_), Clown) => true | |
| 150 | case (Knight(_), Peasant(_)) => true | |
| 151 | case (Knight(_), Clown) => true | |
| 152 | case (Clown, Peasant(_)) => true | |
| 153 | case _ => false | |
| 154 | } | |
| 155 | ||
| 156 | val people = List(Knight("David"), 
 | |
| 157 |                   Peer("Duke", "Norfolk", 84), 
 | |
| 158 |                   Peasant("Christian"), 
 | |
| 159 | King, | |
| 160 | Clown) | |
| 161 | ||
| 162 | println(people.sortWith(superior).mkString("\n"))
 | |
| 163 | ||
| 164 | ||
| 165 | // String interpolations as patterns | |
| 166 | ||
| 167 | val date = "2000-01-01" | |
| 168 | val s"$year-$month-$day" = date | |
| 169 | ||
| 170 | def parse_date(date: String) = date match {
 | |
| 171 | case s"$year-$month-$day" => Some((year.toInt, month.toInt, day.toInt)) | |
| 172 | case s"$day/$month/$year" => Some((year.toInt, month.toInt, day.toInt)) | |
| 173 | case _ => None | |
| 174 | } | |
| 175 | ||
| 176 | ||
| 177 | ||
| 155 | 178 | |
| 217 | 179 | // User-defined Datatypes and Pattern Matching | 
| 218 | 180 | //============================================= | 
| 155 | 181 | |
| 318 | 182 | |
| 183 | ||
| 217 | 184 | abstract class Exp | 
| 218 | 185 | case class N(n: Int) extends Exp // for numbers | 
| 217 | 186 | case class Plus(e1: Exp, e2: Exp) extends Exp | 
| 187 | case class Times(e1: Exp, e2: Exp) extends Exp | |
| 158 | 188 | |
| 218 | 189 | def string(e: Exp) : String = e match {
 | 
| 190 | case N(n) => n.toString | |
| 191 |   case Plus(e1, e2) => "(" + string(e1) + " + " + string(e2) + ")" 
 | |
| 192 |   case Times(e1, e2) => "(" + string(e1) + " * " + string(e2) + ")" 
 | |
| 193 | } | |
| 155 | 194 | |
| 218 | 195 | val e = Plus(N(9), Times(N(3), N(4))) | 
| 196 | println(string(e)) | |
| 197 | ||
| 198 | def eval(e: Exp) : Int = e match {
 | |
| 199 | case N(n) => n | |
| 200 | case Plus(e1, e2) => eval(e1) + eval(e2) | |
| 201 | case Times(e1, e2) => eval(e1) * eval(e2) | |
| 202 | } | |
| 203 | ||
| 204 | def simp(e: Exp) : Exp = e match {
 | |
| 205 | case N(n) => N(n) | |
| 206 |   case Plus(e1, e2) => (simp(e1), simp(e2)) match {
 | |
| 207 | case (N(0), e2s) => e2s | |
| 208 | case (e1s, N(0)) => e1s | |
| 209 | case (e1s, e2s) => Plus(e1s, e2s) | |
| 210 | } | |
| 211 |   case Times(e1, e2) => (simp(e1), simp(e2)) match {
 | |
| 212 | case (N(0), _) => N(0) | |
| 213 | case (_, N(0)) => N(0) | |
| 214 | case (N(1), e2s) => e2s | |
| 215 | case (e1s, N(1)) => e1s | |
| 216 | case (e1s, e2s) => Times(e1s, e2s) | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | println(eval(e)) | |
| 155 | 221 | |
| 218 | 222 | val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9))) | 
| 223 | println(string(e2)) | |
| 224 | println(string(simp(e2))) | |
| 225 | ||
| 226 | // Tokens and Reverse Polish Notation | |
| 227 | abstract class Token | |
| 228 | case class T(n: Int) extends Token | |
| 229 | case object PL extends Token | |
| 230 | case object TI extends Token | |
| 231 | ||
| 232 | def rp(e: Exp) : List[Token] = e match {
 | |
| 233 | case N(n) => List(T(n)) | |
| 234 | case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) | |
| 235 | case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) | |
| 236 | } | |
| 237 | println(string(e2)) | |
| 238 | println(rp(e2)) | |
| 239 | ||
| 240 | def comp(ls: List[Token], st: List[Int]) : Int = (ls, st) match {
 | |
| 241 | case (Nil, st) => st.head | |
| 242 | case (T(n)::rest, st) => comp(rest, n::st) | |
| 243 | case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st) | |
| 244 | case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st) | |
| 245 | } | |
| 246 | ||
| 247 | comp(rp(e), Nil) | |
| 248 | ||
| 249 | def proc(s: String) : Token = s match {
 | |
| 250 | case "+" => PL | |
| 251 | case "*" => TI | |
| 252 | case _ => T(s.toInt) | |
| 253 | } | |
| 254 | ||
| 255 | comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil)
 | |
| 256 | ||
| 155 | 257 | |
| 258 | ||
| 259 | ||
| 217 | 260 | def string(e: Exp) : String = e match {
 | 
| 261 | case N(n) => n.toString | |
| 262 |   case Plus(e1, e2) => "(" + string(e1) + " + " + string(e2) + ")"
 | |
| 263 |   case Times(e1, e2) => "(" + string(e1) + " * " + string(e2) + ")"
 | |
| 264 | } | |
| 155 | 265 | |
| 217 | 266 | val e = Plus(N(9), Times(N(3), N(4))) | 
| 267 | ||
| 268 | println(string(e)) | |
| 155 | 269 | |
| 217 | 270 | def eval(e: Exp) : Int = e match {
 | 
| 271 | case N(n) => n | |
| 272 | case Plus(e1, e2) => eval(e1) + eval(e2) | |
| 273 | case Times(e1, e2) => eval(e1) * eval(e2) | |
| 152 | 274 | } | 
| 275 | ||
| 217 | 276 | eval(e) | 
| 178 | 277 | |
| 217 | 278 | def simp(e: Exp) : Exp = e match {
 | 
| 279 | case N(n) => N(n) | |
| 280 |   case Plus(e1, e2) => (simp(e1), simp(e2)) match {
 | |
| 281 | case (N(0), e2s) => e2s | |
| 282 | case (e1s, N(0)) => e1s | |
| 283 | case (e1s, e2s) => Plus(e1s, e2s) | |
| 284 | } | |
| 285 |   case Times(e1, e2) => (simp(e1), simp(e2)) match {
 | |
| 286 | case (N(0), e2s) => N(0) | |
| 287 | case (e1s, N(0)) => N(0) | |
| 288 | case (N(1), e2s) => e2s | |
| 289 | case (e1s, N(1)) => e1s | |
| 290 | case (e1s, e2s) => Times(e1s, e2s) | |
| 291 | } | |
| 292 | } | |
| 178 | 293 | |
| 152 | 294 | |
| 217 | 295 | val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9))) | 
| 296 | println(string(e2)) | |
| 297 | println(string(simp(e2))) | |
| 67 | 298 | |
| 217 | 299 | // Token and Reverse Polish Notation | 
| 300 | abstract class Token | |
| 301 | case class T(n: Int) extends Token | |
| 302 | case object PL extends Token | |
| 303 | case object TI extends Token | |
| 304 | ||
| 305 | def rp(e: Exp) : List[Token] = e match {
 | |
| 306 | case N(n) => List(T(n)) | |
| 307 | case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) | |
| 308 | case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) | |
| 67 | 309 | } | 
| 310 | ||
| 217 | 311 | def comp(ts: List[Token], stk: List[Int]) : Int = (ts, stk) match {
 | 
| 312 | case (Nil, st) => st.head | |
| 313 | case (T(n)::rest, st) => comp(rest, n::st) | |
| 314 | case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st) | |
| 315 | case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st) | |
| 316 | } | |
| 67 | 317 | |
| 217 | 318 | def exp(ts: List[Token], st: List[Exp]) : Exp = (ts, st) match {
 | 
| 319 | case (Nil, st) => st.head | |
| 320 | case (T(n)::rest, st) => exp(rest, N(n)::st) | |
| 321 | case (PL::rest, n1::n2::st) => exp(rest, Plus(n2, n1)::st) | |
| 322 | case (TI::rest, n1::n2::st) => exp(rest, Times(n2, n1)::st) | |
| 323 | } | |
| 324 | ||
| 325 | exp(toks(e2), Nil) | |
| 326 | ||
| 327 | def proc(s: String) = s match {
 | |
| 328 | case "+" => PL | |
| 329 | case "*" => TI | |
| 330 | case n => T(n.toInt) | |
| 331 | } | |
| 67 | 332 | |
| 155 | 333 | |
| 217 | 334 | string(exp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil))
 | 
| 67 | 335 | |
| 155 | 336 | |
| 337 | ||
| 338 | // Tail recursion | |
| 339 | //================ | |
| 72 | 340 | |
| 67 | 341 | |
| 342 | def fact(n: Long): Long = | |
| 343 | if (n == 0) 1 else n * fact(n - 1) | |
| 344 | ||
| 218 | 345 | def factB(n: BigInt): BigInt = | 
| 346 | if (n == 0) 1 else n * factB(n - 1) | |
| 347 | ||
| 348 | factB(100000) | |
| 349 | ||
| 155 | 350 | fact(10) //ok | 
| 351 | fact(10000) // produces a stackoverflow | |
| 352 | ||
| 353 | def factT(n: BigInt, acc: BigInt): BigInt = | |
| 354 | if (n == 0) acc else factT(n - 1, n * acc) | |
| 355 | ||
| 158 | 356 | factT(10, 1) | 
| 218 | 357 | println(factT(100000, 1)) | 
| 155 | 358 | |
| 359 | // there is a flag for ensuring a function is tail recursive | |
| 360 | import scala.annotation.tailrec | |
| 67 | 361 | |
| 72 | 362 | @tailrec | 
| 67 | 363 | def factT(n: BigInt, acc: BigInt): BigInt = | 
| 364 | if (n == 0) acc else factT(n - 1, n * acc) | |
| 365 | ||
| 366 | ||
| 367 | ||
| 155 | 368 | // for tail-recursive functions the Scala compiler | 
| 71 | 369 | // generates loop-like code, which does not need | 
| 67 | 370 | // to allocate stack-space in each recursive | 
| 155 | 371 | // call; Scala can do this only for tail-recursive | 
| 67 | 372 | // functions | 
| 373 | ||
| 155 | 374 | |
| 375 | ||
| 217 | 376 | // Jumping Towers | 
| 377 | //================ | |
| 378 | ||
| 379 | ||
| 380 | // the first n prefixes of xs | |
| 381 | // for 1 => include xs | |
| 382 | ||
| 218 | 383 | |
| 384 | ||
| 217 | 385 | def moves(xs: List[Int], n: Int) : List[List[Int]] = (xs, n) match {
 | 
| 386 | case (Nil, _) => Nil | |
| 387 | case (xs, 0) => Nil | |
| 388 | case (x::xs, n) => (x::xs) :: moves(xs, n - 1) | |
| 389 | } | |
| 390 | ||
| 391 | ||
| 392 | moves(List(5,1,0), 1) | |
| 393 | moves(List(5,1,0), 2) | |
| 394 | moves(List(5,1,0), 5) | |
| 395 | ||
| 396 | // checks whether a jump tour exists at all | |
| 397 | ||
| 398 | def search(xs: List[Int]) : Boolean = xs match {
 | |
| 399 | case Nil => true | |
| 400 | case (x::xs) => | |
| 401 | if (xs.length < x) true else moves(xs, x).exists(search(_)) | |
| 402 | } | |
| 403 | ||
| 404 | ||
| 405 | search(List(5,3,2,5,1,1)) | |
| 406 | search(List(3,5,1,0,0,0,1)) | |
| 407 | search(List(3,5,1,0,0,0,0,1)) | |
| 408 | search(List(3,5,1,0,0,0,1,1)) | |
| 409 | search(List(3,5,1)) | |
| 410 | search(List(5,1,1)) | |
| 411 | search(Nil) | |
| 412 | search(List(1)) | |
| 413 | search(List(5,1,1)) | |
| 414 | search(List(3,5,1,0,0,0,0,0,0,0,0,1)) | |
| 415 | ||
| 416 | // generates *all* jump tours | |
| 417 | // if we are only interested in the shortes one, we could | |
| 418 | // shortcircut the calculation and only return List(x) in | |
| 419 | // case where xs.length < x, because no tour can be shorter | |
| 420 | // than 1 | |
| 421 | // | |
| 422 | ||
| 423 | def jumps(xs: List[Int]) : List[List[Int]] = xs match {
 | |
| 424 | case Nil => Nil | |
| 425 |   case (x::xs) => {
 | |
| 426 | val children = moves(xs, x) | |
| 218 | 427 | val results = children.map((cs) => jumps(cs).map(x :: _)).flatten | 
| 217 | 428 | if (xs.length < x) List(x) :: results else results | 
| 429 | } | |
| 430 | } | |
| 431 | ||
| 218 | 432 | println(jumps(List(5,3,2,5,1,1)).minBy(_.length)) | 
| 217 | 433 | jumps(List(3,5,1,2,1,2,1)) | 
| 434 | jumps(List(3,5,1,2,3,4,1)) | |
| 435 | jumps(List(3,5,1,0,0,0,1)) | |
| 436 | jumps(List(3,5,1)) | |
| 437 | jumps(List(5,1,1)) | |
| 438 | jumps(Nil) | |
| 439 | jumps(List(1)) | |
| 440 | jumps(List(5,1,2)) | |
| 441 | moves(List(1,2), 5) | |
| 442 | jumps(List(1,5,1,2)) | |
| 443 | jumps(List(3,5,1,0,0,0,0,0,0,0,0,1)) | |
| 444 | ||
| 445 | jumps(List(5,3,2,5,1,1)).minBy(_.length) | |
| 446 | jumps(List(1,3,5,8,9,2,6,7,6,8,9)).minBy(_.length) | |
| 447 | jumps(List(1,3,6,1,0,9)).minBy(_.length) | |
| 448 | jumps(List(2,3,1,1,2,4,2,0,1,1)).minBy(_.length) | |
| 449 | ||
| 450 | ||
| 451 | ||
| 318 | 452 | // Tail Recursion | 
| 453 | //================ | |
| 217 | 454 | |
| 455 | ||
| 318 | 456 | def fact(n: Long): Long = | 
| 457 | if (n == 0) 1 else n * fact(n - 1) | |
| 458 | ||
| 459 | fact(10) //ok | |
| 460 | fact(10000) // produces a stackoverflow | |
| 461 | ||
| 462 | def factT(n: BigInt, acc: BigInt): BigInt = | |
| 463 | if (n == 0) acc else factT(n - 1, n * acc) | |
| 464 | ||
| 465 | factT(10, 1) | |
| 466 | factT(100000, 1) | |
| 467 | ||
| 468 | // there is a flag for ensuring a function is tail recursive | |
| 469 | import scala.annotation.tailrec | |
| 470 | ||
| 471 | @tailrec | |
| 472 | def factT(n: BigInt, acc: BigInt): BigInt = | |
| 473 | if (n == 0) acc else factT(n - 1, n * acc) | |
| 474 | ||
| 475 | ||
| 476 | ||
| 477 | // for tail-recursive functions the Scala compiler | |
| 478 | // generates loop-like code, which does not need | |
| 479 | // to allocate stack-space in each recursive | |
| 480 | // call; Scala can do this only for tail-recursive | |
| 481 | // functions | |
| 482 | ||
| 217 | 483 | |
| 484 | ||
| 485 | ||
| 486 | ||
| 487 | // Sudoku | |
| 488 | //======== | |
| 489 | ||
| 490 | // THE POINT OF THIS CODE IS NOT TO BE SUPER | |
| 491 | // EFFICIENT AND FAST, just explaining exhaustive | |
| 492 | // depth-first search | |
| 493 | ||
| 155 | 494 | |
| 495 | val game0 = """.14.6.3.. | |
| 496 | |62...4..9 | |
| 497 | |.8..5.6.. | |
| 498 | |.6.2....3 | |
| 499 | |.7..1..5. | |
| 500 | |5....9.6. | |
| 501 | |..6.2..3. | |
| 502 | |1..5...92 | |
| 503 |               |..7.9.41.""".stripMargin.replaceAll("\\n", "")
 | |
| 53 | 504 | |
| 155 | 505 | type Pos = (Int, Int) | 
| 506 | val EmptyValue = '.' | |
| 507 | val MaxValue = 9 | |
| 508 | ||
| 509 | val allValues = "123456789".toList | |
| 510 | val indexes = (0 to 8).toList | |
| 511 | ||
| 512 | ||
| 513 | def empty(game: String) = game.indexOf(EmptyValue) | |
| 514 | def isDone(game: String) = empty(game) == -1 | |
| 515 | def emptyPosition(game: String) = | |
| 516 | (empty(game) % MaxValue, empty(game) / MaxValue) | |
| 517 | ||
| 67 | 518 | |
| 155 | 519 | def get_row(game: String, y: Int) = | 
| 520 | indexes.map(col => game(y * MaxValue + col)) | |
| 521 | def get_col(game: String, x: Int) = | |
| 522 | indexes.map(row => game(x + row * MaxValue)) | |
| 523 | ||
| 524 | def get_box(game: String, pos: Pos): List[Char] = {
 | |
| 525 | def base(p: Int): Int = (p / 3) * 3 | |
| 526 | val x0 = base(pos._1) | |
| 527 | val y0 = base(pos._2) | |
| 528 | val ys = (y0 until y0 + 3).toList | |
| 529 | (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue))) | |
| 530 | } | |
| 531 | ||
| 217 | 532 | //get_row(game0, 0) | 
| 533 | //get_row(game0, 1) | |
| 218 | 534 | //get_col(game0, 0) | 
| 535 | //get_box(game0, (3, 1)) | |
| 217 | 536 | |
| 537 | ||
| 155 | 538 | // this is not mutable!! | 
| 539 | def update(game: String, pos: Int, value: Char): String = | |
| 540 | game.updated(pos, value) | |
| 541 | ||
| 542 | def toAvoid(game: String, pos: Pos): List[Char] = | |
| 543 | (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos)) | |
| 544 | ||
| 545 | def candidates(game: String, pos: Pos): List[Char] = | |
| 218 | 546 | allValues.diff(toAvoid(game, pos)) | 
| 155 | 547 | |
| 548 | //candidates(game0, (0,0)) | |
| 549 | ||
| 550 | def pretty(game: String): String = | |
| 218 | 551 |   "\n" + (game.sliding(MaxValue, MaxValue).mkString("\n"))
 | 
| 155 | 552 | |
| 218 | 553 | |
| 155 | 554 | def search(game: String): List[String] = {
 | 
| 555 | if (isDone(game)) List(game) | |
| 556 |   else {
 | |
| 557 | val cs = candidates(game, emptyPosition(game)) | |
| 218 | 558 | cs.par.map(c => search(update(game, empty(game), c))).toList.flatten | 
| 67 | 559 | } | 
| 560 | } | |
| 561 | ||
| 217 | 562 | search(game0).map(pretty) | 
| 563 | ||
| 564 | val game1 = """23.915... | |
| 565 | |...2..54. | |
| 566 | |6.7...... | |
| 567 | |..1.....9 | |
| 568 | |89.5.3.17 | |
| 569 | |5.....6.. | |
| 570 | |......9.5 | |
| 571 | |.16..7... | |
| 572 |               |...329..1""".stripMargin.replaceAll("\\n", "")
 | |
| 573 | ||
| 574 | ||
| 575 | // game that is in the hard category | |
| 576 | val game2 = """8........ | |
| 577 | |..36..... | |
| 578 | |.7..9.2.. | |
| 579 | |.5...7... | |
| 580 | |....457.. | |
| 581 | |...1...3. | |
| 582 | |..1....68 | |
| 583 | |..85...1. | |
| 584 |               |.9....4..""".stripMargin.replaceAll("\\n", "")
 | |
| 585 | ||
| 586 | // game with multiple solutions | |
| 587 | val game3 = """.8...9743 | |
| 588 | |.5...8.1. | |
| 589 | |.1....... | |
| 590 | |8....5... | |
| 591 | |...8.4... | |
| 592 | |...3....6 | |
| 593 | |.......7. | |
| 594 | |.3.5...8. | |
| 595 |               |9724...5.""".stripMargin.replaceAll("\\n", "")
 | |
| 596 | ||
| 597 | ||
| 598 | search(game1).map(pretty) | |
| 599 | search(game3).map(pretty) | |
| 600 | search(game2).map(pretty) | |
| 601 | ||
| 602 | // for measuring time | |
| 603 | def time_needed[T](i: Int, code: => T) = {
 | |
| 604 | val start = System.nanoTime() | |
| 605 | for (j <- 1 to i) code | |
| 606 | val end = System.nanoTime() | |
| 607 | ((end - start) / 1.0e9) + " secs" | |
| 608 | } | |
| 609 | ||
| 610 | time_needed(1, search(game2)) | |
| 611 | ||
| 155 | 612 | // tail recursive version that searches | 
| 158 | 613 | // for all solutions | 
| 614 | ||
| 155 | 615 | def searchT(games: List[String], sols: List[String]): List[String] = games match {
 | 
| 616 | case Nil => sols | |
| 617 |   case game::rest => {
 | |
| 618 | if (isDone(game)) searchT(rest, game::sols) | |
| 619 |     else {
 | |
| 620 | val cs = candidates(game, emptyPosition(game)) | |
| 621 | searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols) | |
| 622 | } | |
| 623 | } | |
| 67 | 624 | } | 
| 625 | ||
| 158 | 626 | searchT(List(game3), List()).map(pretty) | 
| 627 | ||
| 628 | ||
| 155 | 629 | // tail recursive version that searches | 
| 630 | // for a single solution | |
| 158 | 631 | |
| 155 | 632 | def search1T(games: List[String]): Option[String] = games match {
 | 
| 67 | 633 | case Nil => None | 
| 155 | 634 |   case game::rest => {
 | 
| 635 | if (isDone(game)) Some(game) | |
| 636 |     else {
 | |
| 637 | val cs = candidates(game, emptyPosition(game)) | |
| 638 | search1T(cs.map(c => update(game, empty(game), c)) ::: rest) | |
| 639 | } | |
| 640 | } | |
| 67 | 641 | } | 
| 642 | ||
| 158 | 643 | search1T(List(game3)).map(pretty) | 
| 217 | 644 | time_needed(10, search1T(List(game3))) | 
| 645 | ||
| 158 | 646 | |
| 155 | 647 | // game with multiple solutions | 
| 648 | val game3 = """.8...9743 | |
| 649 | |.5...8.1. | |
| 650 | |.1....... | |
| 651 | |8....5... | |
| 652 | |...8.4... | |
| 653 | |...3....6 | |
| 654 | |.......7. | |
| 655 | |.3.5...8. | |
| 656 |               |9724...5.""".stripMargin.replaceAll("\\n", "")
 | |
| 657 | ||
| 158 | 658 | searchT(List(game3), Nil).map(pretty) | 
| 155 | 659 | search1T(List(game3)).map(pretty) | 
| 67 | 660 | |
| 77 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 661 | // Moral: Whenever a recursive function is resource-critical | 
| 158 | 662 | // (i.e. works with large recursion depth), then you need to | 
| 77 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 663 | // write it in tail-recursive fashion. | 
| 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 664 | // | 
| 155 | 665 | // Unfortuantely, Scala because of current limitations in | 
| 666 | // the JVM is not as clever as other functional languages. It can | |
| 77 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 667 | // only optimise "self-tail calls". This excludes the cases of | 
| 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 668 | // multiple functions making tail calls to each other. Well, | 
| 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 669 | // nothing is perfect. | 
| 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 670 | |
| 
3cbe3d90b77f
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
73diff
changeset | 671 | |
| 67 | 672 | |
| 673 | ||
| 71 | 674 | |
| 67 | 675 |