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