changeset 284 | 9a04eb6a2291 |
parent 259 | 43995ea34fe7 |
283:ef5f62bf5987 | 284:9a04eb6a2291 |
---|---|
1 import CW7b._ |
|
2 |
|
1 |
3 |
2 // first test |
4 // first test |
3 |
5 |
4 val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c")) |
6 val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c")) |
5 |
7 |
10 assert(m_urban.getOrElse("2", Nil) == List("a", "a")) |
12 assert(m_urban.getOrElse("2", Nil) == List("a", "a")) |
11 |
13 |
12 // second test |
14 // second test |
13 |
15 |
14 def urban_gb(ratings: List[(String, String)]) = |
16 def urban_gb(ratings: List[(String, String)]) = |
15 ratings.groupBy(_._1).mapValues(_.map(_._2).toSet) |
17 ratings.groupBy(_._1).view.mapValues(_.map(_._2).toSet).toMap |
16 |
18 |
17 def urban_gb2(ratings: List[(String, String)]) = |
19 def urban_gb2(ratings: List[(String, String)]) = |
18 ratings.groupBy(_._1).mapValues(_.map(_._2)) |
20 ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap |
19 |
21 |
20 def urban_ck(ratings: List[(String, String)]) = |
22 def urban_ck(ratings: List[(String, String)]) = |
21 urban_gb(ratings) == groupById(ratings, Map()).mapValues(_.toSet) |
23 urban_gb(ratings) == groupById(ratings, Map()).view.mapValues(_.toSet).toMap |
22 |
24 |
23 |
25 |
24 val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c")) |
26 val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c")) |
25 |
27 |
26 assert(urban_ck(ls2_urban)) |
28 assert(urban_ck(ls2_urban)) |