# HG changeset patch # User Christian Urban # Date 1574145490 0 # Node ID 7b0055205ec900fcc842035974696874a0512141 # Parent cdfb2ce30a3d3f851f0306bcea21112aef9654f4 updated diff -r cdfb2ce30a3d -r 7b0055205ec9 progs/lecture3.scala --- a/progs/lecture3.scala Tue Nov 19 00:40:27 2019 +0000 +++ b/progs/lecture3.scala Tue Nov 19 06:38:10 2019 +0000 @@ -47,7 +47,8 @@ if (n == 0) () else { println(s" Visiting: $n $url") - for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1) + val page = get_page(url) + for (u <- get_all_URLs(page)) crawl(u, n - 1) } } @@ -121,8 +122,9 @@ def search(xs: List[Int]) : Boolean = xs match { case Nil => true - case (x::xs) => - if (xs.length < x) true else moves(xs, x).exists(search(_)) + case x::xs => + if (xs.length < x) true + else moves(xs, x).exists(search(_)) } @@ -138,7 +140,7 @@ search(List(3,5,1,0,0,0,0,0,0,0,0,1)) // generates *all* jump tours -// if we are only interested in the shortes one, we could +// if we are only interested in the shortest one, we could // shortcircut the calculation and only return List(x) in // case where xs.length < x, because no tour can be shorter // than 1 @@ -146,7 +148,7 @@ def jumps(xs: List[Int]) : List[List[Int]] = xs match { case Nil => Nil - case (x::xs) => { + case x::xs => { val children = moves(xs, x) val results = children.map(cs => jumps(cs).map(x :: _)).flatten if (xs.length < x) List(x)::results else results @@ -180,7 +182,7 @@ //======================== -abstract class Colour +sealed abstract class Colour case object Red extends Colour case object Green extends Colour case object Blue extends Colour @@ -196,7 +198,7 @@ // ... a tiny bit more useful: Roman Numerals -abstract class RomanDigit +sealed abstract class RomanDigit case object I extends RomanDigit case object V extends RomanDigit case object X extends RomanDigit @@ -269,7 +271,7 @@ // trees -abstract class Exp +sealed abstract class Exp case class N(n: Int) extends Exp // for numbers case class Plus(e1: Exp, e2: Exp) extends Exp case class Times(e1: Exp, e2: Exp) extends Exp @@ -314,7 +316,7 @@ // Tokens and Reverse Polish Notation -abstract class Token +sealed abstract class Token case class T(n: Int) extends Token case object PL extends Token case object TI extends Token diff -r cdfb2ce30a3d -r 7b0055205ec9 progs/sudoku.scala --- a/progs/sudoku.scala Tue Nov 19 00:40:27 2019 +0000 +++ b/progs/sudoku.scala Tue Nov 19 06:38:10 2019 +0000 @@ -56,11 +56,9 @@ // a list of hard games according to Andrew Coles and Peter Norvig val hard_games = - List("4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......", - "52...6.........7.13...........4..8..6......5...........418.........3..2...87.....", + List("52...6.........7.13...........4..8..6......5...........418.........3..2...87.....", "6.....8.3.4.7.................5.4.7.3..2.....1.6.......2.....5.....8.6......1....", "48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5....", - "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...", "......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.", "6.2.5.........3.4..........43...8....1....2........7..5..27...........81...6.....", ".524.........7.1..............8.2...3.....6...9.5.....1.6.3...........897........", @@ -167,7 +165,9 @@ ".2.3.......6..8.9.83.5........2...8.7.9..5........6..4.......1...1...4.22..7..8.9", ".5..9....1.....6.....3.8.....8.4...9514.......3....2..........4.8...6..77..15..6.", ".....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891..........", - "3...8.......7....51..............36...2..4....7...........6.13..452...........8..") + "3...8.......7....51..............36...2..4....7...........6.13..452...........8..", + "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...", + "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......") // for measuring time