progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 17 Nov 2016 03:10:44 +0000
changeset 55 6610c1dfa8a9
parent 53 9f8751912560
child 56 6fa91be92d0c
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     1
// Scala Lecture 2
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     2
//=================
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     3
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     4
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     5
// Implicits
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     6
//===========
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     7
//
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     8
// for example adding your own methods to Strings:
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     9
// imagine you want to increment strings, like
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    10
//
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    11
//     "HAL".increment
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    12
//
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    13
// you can avoid ugly fudges, like a MyString, by
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    14
// using implicit conversions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    15
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    16
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    17
implicit class MyString(s: String) {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    18
  def increment = for (c <- s) yield (c + 1).toChar 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    19
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    20
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    21
"HAL".increment
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    22
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    23
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    24
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    25
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    26
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    27
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    28
//in Java if something unusually happens, you return null
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    29
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    30
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    31
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    32
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    33
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    34
List(7,2,3,4,5,6).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    35
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    36
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    37
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    38
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    39
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    40
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    41
Some(1).get
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    42
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    43
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    44
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    45
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    46
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    47
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    48
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    49
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    50
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    51
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    52
// getOrElse is to set a default value
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    54
val lst = List(None, Some(1), Some(2), None, Some(3))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    55
for (x <- lst) yield x getOrElse 0
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    56
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    57
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    58
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    59
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    60
// error handling with option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    61
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    62
//  Try(something).getOrElse(what_to_do_in_an_exception)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    63
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    64
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanccc/""").mkString
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    65
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    66
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    67
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    68
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    69
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    70
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    71
Integer.parseInt("12u34")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    72
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    73
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    74
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    75
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    76
val lst = List("12345", "foo", "5432", "bar", "x21")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    77
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    78
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    79
// summing all the numbers
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    80
val sum = lst.flatMap(get_me_an_int(_)).sum
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    82
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    83
// This may not look any better than working with null in Java, but to
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    84
// see the value, you have to put yourself in the shoes of the
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    85
// consumer of the get_me_an_int function, and imagine you didn't
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    86
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    87
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    88
// In Java, if you didn't write this function, you'd have to depend on
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    89
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    90
// for the Java, you might not know that get_me_an_int could return a
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    91
// null, and your code could potentially throw a NullPointerException.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    92
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    93
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    94
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    95
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    96
// Type abbreviations
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    97
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    98
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    99
// some syntactic convenience
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   100
type Pos = (int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   101
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   102
type Board = List[List[Int]]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   103
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   104
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   105
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   106
// No return in Scala
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   107
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   108
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   109
//You should not use "return" in Scala:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   110
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   111
// A return expression, when evaluated, abandons the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   112
// current computation and returns to the caller of the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   113
// function in which return appears."
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   114
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   115
def sq1(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   116
def sq2(x: Int): Int = return x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   117
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   118
def sumq(ls: List[Int]): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   119
  (for (x <- ls) yield (return x * x)).sum[Int]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   120
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   121
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   122
sumq(List(1,2,3,4))
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   124
// last expression in a function is the return statement
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   125
def square(x: Int): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   126
  println(s"The argument is ${x}.")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   127
  x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   128
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   129
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   130
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   131
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   132
// Pattern Matching
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   133
//==================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   134
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   135
// A powerful tool which is supposed to come to Java in a few years
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   136
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   137
// has it for many years ;o)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   138
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   139
// The general schema:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   140
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   141
//    expression match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   142
//       case pattern1 => expression1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   143
//       case pattern2 => expression2
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   144
//       ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   145
//       case patternN => expressionN
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   146
//    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   147
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   148
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   149
// remember
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   150
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
def my_flatten(xs: List[Option[Int]]): List[Int] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   154
  ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   155
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   156
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   157
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   158
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   159
  case Nil => Nil
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   160
  case None::xs => my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   161
  case Some(n)::xs => n::my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   162
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   163
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   164
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   165
// another example
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   166
def get_me_a_string(n: Int): String = n match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   167
  case 0 => "zero"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   168
  case 1 => "one"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   169
  case 2 => "two"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   170
  case _ => "many"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   171
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   172
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   173
// User-defined Datatypes
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   174
//========================
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   175
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   176
abstract class Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   177
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   178
case class Leaf() extends Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   179
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   180
def insert(tr: Tree, n: Int): Tree = tr match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   181
  case Leaf() => Node(n, Leaf(), Leaf())
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   182
  case Node(m, left, right) => 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   183
    if (n == m) Node(m, left, right) 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   184
    else if (n < m) Node(m, insert(left, n), right)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   185
    else Node(m, left, insert(right, n))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   186
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   187
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   188
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   189
val t1 = Node(4, Node(2, Leaf(), Leaf()), Node(7, Leaf(), Leaf()))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   190
insert(t1, 3)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   191
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   192
def balance(tr: Tree): Int = tr match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   193
  case Leaf() => 0
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   194
  case Node(_, left, right) => balance(left) - balance(right)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   195
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   196
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   197
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   198
// another example
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   199
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   200
abstract class Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   201
case class King() extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   202
case class Peer(deg: String, terr: String, succ: Int) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   203
case class Knight(name: String) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   204
case class Peasant(name: String) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   205
case class Clown() extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   206
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   207
def title(p: Person): String = p match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   208
  case King() => "His Majesty the King"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   209
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   210
  case Knight(name) => s"Sir ${name}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   211
  case Peasant(name) => name
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   212
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   213
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   214
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   215
  case (King(), _) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   216
  case (Peer(_,_,_), Knight(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   217
  case (Peer(_,_,_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   218
  case (Peer(_,_,_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   219
  case (Knight(_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   220
  case (Knight(_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   221
  case (Clown(), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   222
  case _ => false
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   223
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   224
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   225
val people = List(Knight("David"), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   226
                  Peer("Duke", "Norfolk", 42), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   227
                  Peasant("Christian"), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   228
                  King(), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   229
                  Clown())
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   230
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   231
println(people.sortWith(superior(_, _)))
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   232
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   233
// Higher-Order Functions
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   234
//========================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   235
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   236
// functions can take functions as arguments
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   237
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   238
val lst = (1 to 10).toList
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   239
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   240
def even(x: Int): Boolean = x % 2 == 0
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   241
def odd(x: Int): Boolean = x % 2 == 1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   242
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   243
lst.filter(x => even(x))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   244
lst.filter(even(_))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   245
lst.filter(even)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   246
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   247
lst.find(_ > 8)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   248
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   249
def square(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   250
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   251
lst.map(square)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   252
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   253
lst.map(square).filter(_ > 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   254
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   255
lst.map(square).filter(_ > 4).map(square)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   256
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   257
// in my collatz.scala
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   258
//(1 to bnd).map(i => (collatz(i), i)).maxBy(_._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   259
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   260
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   261
// type of functions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   262
def my_map_int(lst: List[Int], f: Int => Int): List[Int] = lst match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   263
  case Nil => Nil
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   264
  case x::xs => f(x)::my_map_int(xs, f)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   265
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   266
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   267
my_map_int(lst, square)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   268
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   269
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   270
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   271
  case Nil => 0
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   272
  case x::xs => f(x) + sumOf(f, xs)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   273
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   274
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   275
def sum_squares(lst: List[Int]) = sumOf(square, lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   276
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   277
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   278
sum_squares(lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   279
sum_cubes(lst)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   280
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   281
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   282
// Sudoku
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   283
//========
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   284
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   285
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   286
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   287
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   288
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   289
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   290
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   291
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   292
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   293
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   294
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   295
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   296
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   297
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   298
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   299
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   300
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   301
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   302
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   303
def empty(game: String) = game.indexOf(EmptyValue)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   304
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   305
def isDone(game: String) = empty(game) == -1 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   306
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   307
def row(game: String, y: Int): List[Char] = indexes.map(col => game(y * MaxValue + col))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   308
def col(game: String, x: Int): List[Char] = indexes.map(row => game(x + row * MaxValue))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   309
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   310
def box(game: String, pos: Pos): List[Char] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   311
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   312
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   313
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   314
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   315
    (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   316
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   317
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   318
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   319
//row(game0, 0)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   320
//row(game0, 1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   321
//box(game0, (3,1))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   322
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   323
def update(game: String, pos: Int, value: Char): String = game.updated(pos, value)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   324
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   325
def toAvoid(game: String, pos: Pos): List[Char] = 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   326
  (col(game, pos._1) ++ row(game, pos._2) ++ box(game, pos)).distinct
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   327
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   328
def candidates(game: String, pos: Pos): List[Char] = allValues diff toAvoid(game,pos)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   329
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   330
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   331
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   332
def pretty(game: String): String = "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   333
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   334
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   335
  if (isDone(game)) List(game)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   336
  else 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   337
    candidates(game, emptyPosition(game)).par.map(c => search(update(game, empty(game), c))).toList.flatten
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   338
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   339
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   340
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   341
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   342
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   343
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   344
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   345
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   346
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   347
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   348
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   349
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   350
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   351
// game that is in the hard category
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   352
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   353
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   354
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   355
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   356
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   357
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   358
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   359
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   360
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   361
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   362
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   363
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   364
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   365
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   366
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   367
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   368
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   369
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   370
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   371
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   372
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   373
search(game0).map(pretty)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   374
search(game1).map(pretty)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   375
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   376
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   377
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   378
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   379
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   380
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   381
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   382
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   383
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   384
search(game2).map(pretty)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   385
search(game3).distinct.map(pretty).length
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   386
time_needed(3, search(game2))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   387
time_needed(3, search(game3))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   388
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   389
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   390
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   391
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   392