-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andreas Roehler <[email protected]>
- Loading branch information
1 parent
73f5154
commit 3de2a98
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |