object ScrabbleScore {
private def letterValue(c: Char): Int = c match {
case 'A' | 'E' | 'I' | 'O' | 'U' | 'L' | 'N' | 'R' | 'S' | 'T' => 1
case 'D' | 'G' => 2
case 'B' | 'C' | 'M' | 'P' => 3
case 'F' | 'H' | 'V' | 'W' | 'Y' => 4
case 'K' => 5
case 'J' | 'X' => 8
case 'Q' | 'Z' => 10
}
def score(word: String): Int = word.toUpperCase.map(letterValue).sum
}
This approach starts be defining a private method that uses a match
for returning a score based on the letter.
Note that the most likely cases values are in the first arm, and the least likely values are in the last arm.
The score()
method passes each uppercased character to the map()
method, which passes each character to the lookup method,
which returns the score for the character.
The score()
method returns the result of passing all of the scores to the sum()
method.
9th Oct 2024
·
Found it useful?