Skip to content

Commit

Permalink
solution2.1.7.9_as_function added
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Dec 11, 2023
1 parent 73f5154 commit 3de2a98
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions chapter02/worksheets/solution2.1.7.9_as_function.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** author: Andreas Röhler */
/** author: Sergei Winitzki */

/**
Exercise 2.1.7.9
Given a Seq[(String, Int)] showing a list of purchased items (where item names
may repeat), compute a Map[String, Int] showing the total counts. So, for the input:
Seq(("apple", 2), ("pear", 3), ("apple", 5), ("lemon", 2), ("apple", 3))
the output must be Map("apple" -> 10, "pear" -> 3, "lemon" -> 2).
Hint: use groupBy, map, sum.
*/

def totalCount(arg: Seq[(String, Int)]) = {
// val a: Seq[(String, Int)] = Seq(("pear", 2), ("apple", 2), ("apple", 6), ("pear", 1), ("lemon", 4), ("apple", 2), ("apple", 2))
arg.groupBy(s => s._1).map { case (x, y) => (x, y.map(_._2).sum) }

// scala> totalCount(Seq(("pear", 2), ("apple", 2), ("apple", 6), ("pear", 1), ("lemon", 4), ("apple", 2), ("apple", 2)))
// res37: scala.collection.immutable.Map[String,Int] = Map(pear -> 3, apple -> 12, lemon -> 4)

0 comments on commit 3de2a98

Please sign in to comment.