Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to High Scores in the Python Track

Published at Nov 30 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Manage a game player's High Score list.

Your task is to build a high-score component of the classic Frogger game, one of the highest selling and addictive games of all time, and a classic of the arcade era. Your task is to write methods that return the highest score from the list, the last added score, the three highest scores, and a report on the difference between the last and the highest scores.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the exercism/python/<exerciseName> directory.

For example, if you're submitting bob.py for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/python/bob/bob.py.

For more detailed information about running tests, code style and linting, please see the help page.

Source

Tribute to the eighties' arcade game Frogger

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

high_scores_test.py

import unittest

from high_scores import HighScores


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0


class HighScoreTest(unittest.TestCase):
    def test_list_of_scores(self):
        scores = [30, 50, 20, 70]
        expected = [30, 50, 20, 70]
        self.assertEqual(HighScores(scores).scores, expected)

    def test_latest_score(self):
        scores = [100, 0, 90, 30]
        expected = 30
        self.assertEqual(HighScores(scores).latest(), expected)

    def test_highest_score(self):
        scores = [40, 100, 70]
        expected = 100
        self.assertEqual(HighScores(scores).highest(), expected)

    def test_top_3_scores(self):
        scores = [50, 30, 10]
        expected = [50, 30, 10]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_personal_bests_highest_to_lowest(self):
        scores = [20, 10, 30]
        expected = [30, 20, 10]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_personal_bests_when_there_is_a_tie(self):
        scores = [40, 20, 40, 30]
        expected = [40, 40, 30]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_personal_bests_when_there_are_less_than_3(self):
        scores = [30, 70]
        expected = [70, 30]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_personal_bests_when_there_is_only_one(self):
        scores = [40]
        expected = [40]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_personal_bests_from_a_long_list(self):
        scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
        expected = [100, 90, 70]
        self.assertEqual(HighScores(scores).top(), expected)

    def test_message_for_new_personal_best(self):
        scores = [20, 40, 0, 30, 70]
        expected = "Your latest score was 70. That's your personal best!"
        self.assertEqual(HighScores(scores).report(), expected)

    def test_message_when_latest_score_is_not_the_highest_score(self):
        scores = [20, 100, 0, 30, 70]
        expected = (
            "Your latest score was 70. That's 30 short of your personal best!"
        )
        self.assertEqual(HighScores(scores).report(), expected)

    def test_message_for_repeated_personal_best(self):
        scores = [20, 70, 50, 70, 30]
        expected = (
            "Your latest score was 30. That's 40 short of your personal best!"
        )
        self.assertEqual(HighScores(scores).report(), expected)


if __name__ == "__main__":
    unittest.main()
import heapq


class HighScores(object):
    def __init__(self, scores):
        self.scores = scores

    def highest(self):
        return max(self.scores)

    def latest(self):
        return self.scores[-1]

    def top(self):
        return heapq.nlargest(3, self.scores)

    def report(self):
        return f"{self.latest_score_message()} {self.personal_best_message()}"

    def latest_score_message(self):
        return f"Your latest score was {self.latest()}."

    def personal_best_message(self):
        if self.amount_short() == 0:
            return "That's your personal best!"
        else:
            return f"That's {self.amount_short()} short of your personal best!"

    def amount_short(self):
        return self.highest() - self.latest()

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?