1 import M2 ._ |
|
2 |
|
3 |
|
4 // first test |
|
5 |
|
6 val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c")) |
|
7 |
|
8 val m_urban = groupById(ls1_urban, Map()) |
|
9 |
|
10 assert(m_urban.getOrElse("1", Nil).count(_ == "c") == 2) |
|
11 assert(m_urban.getOrElse("1", Nil).count(_ == "a") == 1) |
|
12 assert(m_urban.getOrElse("2", Nil) == List("a", "a")) |
|
13 |
|
14 // second test |
|
15 |
|
16 def urban_gb(ratings: List[(String, String)]) = |
|
17 ratings.groupBy(_._1).view.mapValues(_.map(_._2).toSet).toMap |
|
18 |
|
19 def urban_gb2(ratings: List[(String, String)]) = |
|
20 ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap |
|
21 |
|
22 def urban_ck(ratings: List[(String, String)]) = |
|
23 urban_gb(ratings) == groupById(ratings, Map()).view.mapValues(_.toSet).toMap |
|
24 |
|
25 |
|
26 val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c")) |
|
27 |
|
28 assert(urban_ck(ls2_urban)) |
|
29 |
|
30 // thrird test |
|
31 |
|
32 val ls3_urban = (11 to 1000 by 5).map(_.toString).toList |
|
33 val ls4_urban = ls3_urban zip ls3_urban.tail |
|
34 val ls5_urban = ls4_urban ::: ls4_urban.reverse |
|
35 |
|
36 assert(urban_ck(ls5_urban)) |
|
37 |
|
38 |
|
39 /* |
|
40 import io.Source |
|
41 import scala.util._ |
|
42 |
|
43 def urban_get_csv_file(name: String) : List[String] = { |
|
44 val csv = Source.fromFile(name) |
|
45 csv.mkString.split("\n").toList.drop(1) |
|
46 } |
|
47 |
|
48 def urban_process_ratings(lines: List[String]) : List[(String, String)] = { |
|
49 for (cols <- lines.map(_.split(",").toList); |
|
50 if (cols(2).toFloat >= 4)) yield (cols(0), cols(1)) |
|
51 } |
|
52 |
|
53 val urban_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv").take(1000)) |
|
54 |
|
55 assert(urban_ck(urban_ratings)) |
|
56 */ |
|