Problem overview
The method safeLookup gets scores, a scala.collection.mutable.Map[String, Int] from names to scores, and a name. It returns the score of that name, or 0 when the name isn't a key.
With {"Ana": 90, "Ben": 75, "Cara": 88}, the name "Ben" gives 75 and the name "Dan" gives 0. An empty map gives 0 for every name.
Approach
The direct lookup scores(name) throws a NoSuchElementException when the key is missing. For a missing name the program would crash instead of answering 0. You could check scores.contains(name) first and branch, but getOrElse does both jobs in one call.
Step by step
There is one step: return scores.getOrElse(name, 0). The call returns the value stored under name when the key exists, and the default, 0, when it doesn't.
The solution in Scala
class Solution {
def safeLookup(scores: scala.collection.mutable.Map[String, Int], name: String): Int = {
return scores.getOrElse(name, 0)
}
}
Language details:
getOrElse(key, default)takes the default as a by-name parameter, so it is only computed when the key is missing. A plain0costs nothing either way.- Scala has a third way to read a map:
get(key)returns anOption, which isSome(value)orNone.getOrElseon the map is the same asscores.get(name).getOrElse(0)in one step. - The result type is
Int, matching the map's value type and the default.
Edge cases and common mistakes
- Using
scores(name). It works for "Ben" but throwsNoSuchElementException: key not found: Danfor "Dan". - Letter case. Keys are compared exactly. With the map above,
"ana"is not a key, so the answer is 0 even though"Ana"has 90. - A stored 0. A name whose score is really 0 also gives 0. The answer is the same as for a missing name, which is what this task asks for.
- The map isn't changed.
getOrElseonly reads. It doesn't add the missing key.
Why a default helps
A default value keeps the rest of a program simple. With getOrElse(name, 0), the result is always an Int, so it can go straight into arithmetic. Adding up the scores of the names "Ana", "Dan" and "Ben" from a map holding only Ana's 90 gives 90: the unknown names count as 0 and nothing crashes. Without a default, every caller would need its own check for missing names.
Reading the lesson's examples
In the first example, "Ben" is a key, so getOrElse returns his stored 75 and ignores the default. In the second, "Dan" isn't a key, so the default 0 comes back. The third example has an empty map, which has no keys at all, so every name takes the default. One call covers all three cases without any if.
Other ways to write it
Each of these gives the same answers as getOrElse:
if (scores.contains(name)) scores(name) else 0
scores.get(name) match {
case Some(v) => v
case None => 0
}
With {"Ana": 90}, the first returns 0 for "Dan" and the second returns 90 for "Ana". Both are longer, and the first looks the key up twice. getOrElse says "this value, or that default" in one call and is the usual choice.
Remember that the method only answers the question. It never adds the missing name to scores, so the map is the same after the call as before it.
Testing
safeLookup({"Ana": 90, "Ben": 75, "Cara": 88}, "Ben") -> 75
safeLookup({"Ana": 90, "Ben": 75, "Cara": 88}, "Dan") -> 0
safeLookup({"Ana": 90, "Ben": 75, "Cara": 88}, "ana") -> 0
safeLookup({}, "Ana") -> 0
