Scrabble Score
Score a word by summing the Scrabble values of its letters.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: scrabble_score.nomi
//# A word's Scrabble score.
/// The sum of the letter values in `word`, in either case. Anything that is
/// not a Latin letter scores nothing.
pub fn score(word: String) -> Int
word
|> String.fold_case()
|> Iter.map(letter_value)
|> Iter.sum()
end
fn letter_value(letter: String) -> Int
case letter
"a" or "e" or "i" or "o" or "u" or "l" or "n" or "r" or "s" or "t" -> 1
"d" or "g" -> 2
"b" or "c" or "m" or "p" -> 3
"f" or "h" or "v" or "w" or "y" -> 4
"k" -> 5
"j" or "x" -> 8
"q" or "z" -> 10
_ -> 0
end
end
// FILE: scrabble_score_test.nomi
import scrabble_score
test "score"
[
("lowercase letter", "a", 1),
("uppercase letter", "A", 1),
("valuable letter", "f", 4),
("short word", "at", 2),
("short, valuable word", "zoo", 12),
("medium word", "street", 6),
("medium, valuable word", "quirky", 22),
("long, mixed-case word", "OxyphenButazone", 41),
("english-like word", "pinata", 8),
("empty input", "", 0),
("entire alphabet available", "abcdefghijklmnopqrstuvwxyz", 87),
]
|> subtests (description, word, expected), description
assert scrabble_score.score(word) == expected
end
endok score (11 cases)
test result: ok. 11 passed, 0 failed
How it’s written
Section titled “How it’s written”- The letter table is one
caseover one-letter strings, each value one arm with its letters joined byor(style §25). AMapbuilt from the table was the other option; thecasereads as the problem’s table and needs no lookup that may miss. - The word is case-folded once with
String.fold_case, then iterated as graphemes, so the table lists lowercase letters only. - The total is
Iter.sum(style §33). - Anything not in the table scores 0, which the canonical data does not test.