marking2/danube_test3.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 29 Oct 2019 09:54:52 +0000
changeset 278 0c2481cd8b1c
parent 259 43995ea34fe7
child 284 9a04eb6a2291
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
259
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// first test 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
val m_urban = groupById(ls1_urban, Map())
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
assert(m_urban.getOrElse("1", Nil).count(_ == "c") == 2)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
assert(m_urban.getOrElse("1", Nil).count(_ == "a") == 1)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
assert(m_urban.getOrElse("2", Nil) == List("a", "a"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// second test
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def urban_gb(ratings: List[(String, String)]) = 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  ratings.groupBy(_._1).mapValues(_.map(_._2).toSet) 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def urban_gb2(ratings: List[(String, String)]) = 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  ratings.groupBy(_._1).mapValues(_.map(_._2)) 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def urban_ck(ratings: List[(String, String)]) =
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  urban_gb(ratings) == groupById(ratings, Map()).mapValues(_.toSet)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
assert(urban_ck(ls2_urban))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// thrird test
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
val ls3_urban = (1 to 1000).map(_.toString).toList
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
val ls4_urban = ls3_urban zip ls3_urban.tail
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
val ls5_urban = ls4_urban ::: ls4_urban.reverse
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
assert(urban_ck(ls5_urban))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
/*
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
import io.Source
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
import scala.util._
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
def urban_get_csv_file(name: String) : List[String] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  val csv = Source.fromFile(name)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  csv.mkString.split("\n").toList.drop(1)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  for (cols <- lines.map(_.split(",").toList); 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
val urban_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv").take(1000))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
assert(urban_ck(urban_ratings))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
*/