403
|
1 |
import M2._
|
263
|
2 |
|
284
|
3 |
|
|
4 |
// first test
|
263
|
5 |
|
284
|
6 |
val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c"))
|
|
7 |
|
|
8 |
val m_urban = groupById(ls1_urban, Map())
|
263
|
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"))
|
284
|
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 = (1 to 1000).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 |
|
384
|
38 |
// fourth test !!!
|
284
|
39 |
|
|
40 |
/*
|
384
|
41 |
|
|
42 |
val ls6_urban = (10 to 500 by 10).map(_.toString).toList
|
|
43 |
val ls7_urban = ls6_urban zip ls6_urban.tail
|
|
44 |
val ls8_urban = ls7_urban ::: ls7_urban.reverse
|
284
|
45 |
|
384
|
46 |
assert(urban_ck(ls8_urban))
|
284
|
47 |
*/
|