diff -r ed63dca8068a -r 4113d4d8cf62 main_marking2/danube_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_marking2/danube_test3.scala Mon Jan 25 00:21:00 2021 +0000 @@ -0,0 +1,56 @@ +import CW7b._ + + +// first test + +val ls1_urban = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c")) + +val m_urban = groupById(ls1_urban, Map()) + +assert(m_urban.getOrElse("1", Nil).count(_ == "c") == 2) +assert(m_urban.getOrElse("1", Nil).count(_ == "a") == 1) +assert(m_urban.getOrElse("2", Nil) == List("a", "a")) + +// second test + +def urban_gb(ratings: List[(String, String)]) = + ratings.groupBy(_._1).view.mapValues(_.map(_._2).toSet).toMap + +def urban_gb2(ratings: List[(String, String)]) = + ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap + +def urban_ck(ratings: List[(String, String)]) = + urban_gb(ratings) == groupById(ratings, Map()).view.mapValues(_.toSet).toMap + + +val ls2_urban = List(("1", "a"), ("1", "b"), ("2", "x"), ("3", "a"), ("2", "y"), ("3", "c")) + +assert(urban_ck(ls2_urban)) + +// thrird test + +val ls3_urban = (11 to 1000 by 5).map(_.toString).toList +val ls4_urban = ls3_urban zip ls3_urban.tail +val ls5_urban = ls4_urban ::: ls4_urban.reverse + +assert(urban_ck(ls5_urban)) + + +/* +import io.Source +import scala.util._ + +def urban_get_csv_file(name: String) : List[String] = { + val csv = Source.fromFile(name) + csv.mkString.split("\n").toList.drop(1) +} + +def urban_process_ratings(lines: List[String]) : List[(String, String)] = { + for (cols <- lines.map(_.split(",").toList); + if (cols(2).toFloat >= 4)) yield (cols(0), cols(1)) +} + +val urban_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv").take(1000)) + +assert(urban_ck(urban_ratings)) +*/