High Scores
Read a player’s list of game scores: all of them, the latest, the best, and the top three.
Adapted from Exercism’s problem specification (MIT). Listed under Collections & Algorithms.
// FILE: high_scores.nomi
//# A player's scores in a game, oldest first, and what can be read from
//# them.
/// Every score, oldest first.
pub fn scores(scores: List<Int>) -> List<Int>
scores
end
/// The most recent score, or `None` before the first game.
pub fn latest(scores: List<Int>) -> Maybe<Int>
Iter.last(scores)
end
/// The highest score, or `None` before the first game.
pub fn personal_best(scores: List<Int>) -> Maybe<Int>
Iter.max(scores)
end
/// The three highest scores, highest first, or all of them when there are
/// fewer than three.
pub fn personal_top_three(scores: List<Int>) -> List<Int>
scores
|> Iter.sort(.Descending)
|> Iter.take(3)
|> Iter.to_list()
end
// FILE: high_scores_test.nomi
import high_scores
test "List of scores"
assert high_scores.scores([30, 50, 20, 70]) == [30, 50, 20, 70]
end
test "Latest score"
assert high_scores.latest([100, 0, 90, 30]) == Some(30)
end
test "Personal best"
assert high_scores.personal_best([40, 100, 70]) == Some(100)
end
group "Top 3 scores"
test "personal top three"
[
(
"Personal top three from a list of scores",
[10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70],
[100, 90, 70],
),
("Personal top highest to lowest", [20, 10, 30], [30, 20, 10]),
("Personal top when there is a tie", [40, 20, 40, 30], [40, 40, 30]),
("Personal top when there are less than 3", [30, 70], [70, 30]),
("Personal top when there is only one", [40], [40]),
]
|> subtests (description, scores, expected), description
assert high_scores.personal_top_three(scores) == expected
end
end
test "Latest score after personal top scores"
scores = [70, 50, 20, 30]
_ = high_scores.personal_top_three(scores)
assert high_scores.latest(scores) == Some(30)
end
test "Scores after personal top scores"
scores = [30, 50, 20, 70]
_ = high_scores.personal_top_three(scores)
assert high_scores.scores(scores) == [30, 50, 20, 70]
end
test "Latest score after personal best"
scores = [20, 70, 15, 25, 30]
_ = high_scores.personal_best(scores)
assert high_scores.latest(scores) == Some(30)
end
test "Scores after personal best"
scores = [20, 70, 15, 25, 30]
_ = high_scores.personal_best(scores)
assert high_scores.scores(scores) == [20, 70, 15, 25, 30]
end
endok List of scores
ok Latest score
ok Personal best
ok Top 3 scores / personal top three (5 cases)
ok Top 3 scores / Latest score after personal top scores
ok Top 3 scores / Scores after personal top scores
ok Top 3 scores / Latest score after personal best
ok Top 3 scores / Scores after personal best
test result: ok. 12 passed, 0 failed
How it’s written
Section titled “How it’s written”- The scores are a plain
List<Int>, oldest first, and each property is a file-level function over it. AHighScoresstruct wrapping one list would add a constructor and a field read and nothing else.scoresanswers the list it is given, kept because the canonical data names the property. latestandpersonal_bestanswerMaybe<Int>, since a player who has not played has neither; they areIter.lastandIter.max. The canonical lists are never empty, so the tests assertSome(...).personal_top_threeisIter.sort(.Descending),Iter.take(3)andIter.to_list, which keeps ties and answers fewer than three when there are fewer.- The four “after” cases check that reading the top three or the best leaves the scores as they were. Values are immutable, so they hold by construction; the tests still call the reader first and then check, as the canonical cases describe.
- The canonical “Top 3 scores” group is a
groupof that name. Its fivepersonalTopThreerows are onesubteststable, and the four “after” cases are tests of their own, since each checks a different reader.