Skip to content

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
end
  • The scores are a plain List<Int>, oldest first, and each property is a file-level function over it. A HighScores struct wrapping one list would add a constructor and a field read and nothing else. scores answers the list it is given, kept because the canonical data names the property.
  • latest and personal_best answer Maybe<Int>, since a player who has not played has neither; they are Iter.last and Iter.max. The canonical lists are never empty, so the tests assert Some(...).
  • personal_top_three is Iter.sort(.Descending), Iter.take(3) and Iter.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 group of that name. Its five personalTopThree rows are one subtests table, and the four “after” cases are tests of their own, since each checks a different reader.