main_testing2/danube_test3.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 26 Dec 2022 16:49:53 +0000
changeset 458 d9f8245d0861
parent 403 ffce7b61b446
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     1
import M2._
263
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
     3
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
     4
// first test 
263
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
     6
val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c"))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
     7
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
     8
val m_urban = groupById(ls1_urban, Map())
263
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
assert(m_urban.getOrElse("1", Nil).count(_ == "c") == 2)
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
assert(m_urban.getOrElse("1", Nil).count(_ == "a") == 1)
5a16bcb4105e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
assert(m_urban.getOrElse("2", Nil) == List("a", "a"))
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    13
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    14
// second test
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    15
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    16
def urban_gb(ratings: List[(String, String)]) = 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    17
  ratings.groupBy(_._1).view.mapValues(_.map(_._2).toSet).toMap 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    18
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    19
def urban_gb2(ratings: List[(String, String)]) = 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    20
  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    21
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    22
def urban_ck(ratings: List[(String, String)]) =
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    23
  urban_gb(ratings) == groupById(ratings, Map()).view.mapValues(_.toSet).toMap
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    24
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    25
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    26
val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c"))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    27
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    28
assert(urban_ck(ls2_urban))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    29
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    30
// thrird test
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    31
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    32
val ls3_urban = (1 to 1000).map(_.toString).toList
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    33
val ls4_urban = ls3_urban zip ls3_urban.tail
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    34
val ls5_urban = ls4_urban ::: ls4_urban.reverse
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    35
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    36
assert(urban_ck(ls5_urban))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    37
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    38
// fourth test !!!
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    39
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    40
/*
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    41
 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    42
val ls6_urban = (10 to 500 by 10).map(_.toString).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    43
val ls7_urban = ls6_urban zip ls6_urban.tail
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    44
val ls8_urban = ls7_urban ::: ls7_urban.reverse
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    45
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    46
assert(urban_ck(ls8_urban)) 
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
    47
*/