Skip to content

Commit

Permalink
high-scores: Implement new exercise (#1572)
Browse files Browse the repository at this point in the history
* high-scores: Implement new exercise

* Adapt to default flake8 line length of 80

* Switch properties to methods

* Add topics
  • Loading branch information
behrtam authored and cmccandless committed Oct 29, 2018
1 parent c61caaa commit a57699a
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Expand Up @@ -168,6 +168,19 @@
]
},
{
"uuid": "574d6323-5ff5-4019-9ebe-0067daafba13",
"slug": "high-scores",
"core": false,
"unlocked_by": null,
"difficulty": 1,
"topics": [
"control-flow (if-else statements)",
"sequences",
"text_formatting"
]
},
{
"uuid": "505e7bdb-e18d-45fd-9849-0bf33492efd9",
"slug": "run-length-encoding",
"uuid": "505e7bdb-e18d-45fd-9849-0bf33492efd9",
"core": false,
Expand Down
22 changes: 22 additions & 0 deletions exercises/high-scores/README.md
@@ -0,0 +1,22 @@
# High Scores

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](http://exercism.io/languages/python).

## 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.
21 changes: 21 additions & 0 deletions exercises/high-scores/example.py
@@ -0,0 +1,21 @@
class HighScores(object):
def __init__(self, scores):
self.scores = scores

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

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

def top(self):
return sorted(self.scores, reverse=True)[:3]

def report(self):
difference = self.highest() - self.latest()
result_qualifier = (
"" if difference <= 0 else "{} short of ".format(difference)
)
return "Your latest score was {}. That's {}your personal best!".format(
self.latest(), result_qualifier
)
3 changes: 3 additions & 0 deletions exercises/high-scores/high_scores.py
@@ -0,0 +1,3 @@
class HighScores(object):
def __init__(self, scores):
pass
76 changes: 76 additions & 0 deletions exercises/high-scores/high_scores_test.py
@@ -0,0 +1,76 @@
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()

0 comments on commit a57699a

Please sign in to comment.