444
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// Task 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
2 + 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
1 / 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
1.0 / 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
1 / 2.0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
1 / 0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
1.0 / 0.0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
true == false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
true && false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
1 > 1.0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
"12345".length
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
List(1,2,1).size
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
Set(1,2,1).size
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
List(1) == List(1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
Set(1,2,3) == Set(3,2,1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
Array(1) == Array(1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
Array(1).sameElements(Array(1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
// Task 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
val z = 42
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
z = z + 1 . // error
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
val x = 2 * z
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
val z = 466 // old z not accessible anymore
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
println(x)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// Task 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
println("Hello " ++ "World")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
List(1,2,3,4).mkString("\n")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
List(1,2,3,4).mkString("(", "|", ")")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// Task 4
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
def miles2meters(m: Int): Int = m * 1609
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
// Task 5
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
val s1 = "foo"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
val s2 = "bar"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
val s3 = "foobar"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
if (s1 == s2) print("equal") else print("unequal")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
if (s1 ++ s2 == s3) print("equal") else print("unequal")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
// Task 6
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
for (x <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
y <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
z <- (1 to 5).toList) yield (x,y,z)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
for (x <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
y <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
z <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
if (x + y + z) % 3 == 0) yield (x,y,z)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
(for (x <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
y <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
z <- (1 to 5).toList;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
if (x + y + z) % 3 == 0) yield (x,y,z)).sortBy(_._2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
// Task 7
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
// first version with using an accumulator
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
def cnt(xs: List[Int], acc: List[(Int, Int)] = Nil) : List[(Int, Int)] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
(xs, acc) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
case (Nil, acc) => acc.reverse
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
case (x::xs, Nil) => cnt(xs, (x, 1)::Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
case (x::xs, (y, n)::ys) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
if (x == y) cnt(xs, (y, n + 1)::ys)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
else cnt(xs, (x, 1)::(y, n)::ys)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
def toStr(p: (Int, Int)) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
if (p._2 == 1) s"${p._1}" else s"${p._2} x ${p._1}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
def pp_list(xs: List[Int]) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
cnt(xs).map(toStr)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
pp_list(List(1,1,1,2,3,3)) // List(3 x 1, 2, 2 x 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
pp_list(List(1,2,3,4,4,4)) // List(1, 2, 3, 3 x 4)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
pp_list(List(1,1,1,1,1,1)) // List(6 x 1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
pp_list(List(1,1,1,2,1,1)) // List(3 x 1, 2, 2 x 1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
// second version with just a simple counter
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
def cnt2(xs: List[Int], n : Int = 0) : List[(Int, Int)] = xs match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case Nil => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case x::Nil => (x, n + 1)::Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case x::y::tail =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
if (x == y) cnt2(y::tail, n + 1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
else (x, n + 1)::cnt2(y::tail, 0)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
def pp_list2(xs: List[Int]) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
cnt2(xs).map(toStr)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
pp_list2(List(1,1,1,2,3,3)) // List(3 x 1, 2, 2 x 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
pp_list2(List(1,2,3,4,4,4)) // List(1, 2, 3, 3 x 4)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
pp_list2(List(1,1,1,1,1,1)) // List(6 x 1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
pp_list2(List(1,1,1,2,1,1)) // List(3 x 1, 2, 2 x 1) |