I was under a strong sleep deprivation when I wrote that solution, I think it can be done in a simpler way:
object Bookstore2 { type Book = (Char, Int) def parseBook (s: String): Book = { val Array(c, q) = s.split(' ') (c.head, q.toInt) } def countBooks (books: Seq[Book]): Int = { (books map (_._2)).sum } def solve (cat: Seq[String], stock: Seq[String]): String = { val stocklist = stock map parseBook groupBy (_._1) mapValues countBooks val cats = for { c <- cat q = stocklist.getOrElse(c.head, 0) } yield s"($c : $q)" cats mkString " - " } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I was under a strong sleep deprivation when I wrote that solution, I think it can be done in a simpler way: