Skip to content

Commit a57699a

Browse files
behrtamcmccandless
authored andcommitted
high-scores: Implement new exercise (exercism#1572)
* high-scores: Implement new exercise * Adapt to default flake8 line length of 80 * Switch properties to methods * Add topics
1 parent c61caaa commit a57699a

5 files changed

Lines changed: 135 additions & 0 deletions

File tree

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@
168168
]
169169
},
170170
{
171+
"uuid": "574d6323-5ff5-4019-9ebe-0067daafba13",
172+
"slug": "high-scores",
173+
"core": false,
174+
"unlocked_by": null,
175+
"difficulty": 1,
176+
"topics": [
177+
"control-flow (if-else statements)",
178+
"sequences",
179+
"text_formatting"
180+
]
181+
},
182+
{
183+
"uuid": "505e7bdb-e18d-45fd-9849-0bf33492efd9",
171184
"slug": "run-length-encoding",
172185
"uuid": "505e7bdb-e18d-45fd-9849-0bf33492efd9",
173186
"core": false,

exercises/high-scores/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# High Scores
2+
3+
Manage a game player's High Score list.
4+
5+
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.
6+
7+
## Submitting Exercises
8+
9+
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
10+
11+
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`.
12+
13+
14+
For more detailed information about running tests, code style and linting,
15+
please see the [help page](http://exercism.io/languages/python).
16+
17+
## Source
18+
19+
Tribute to the eighties' arcade game Frogger
20+
21+
## Submitting Incomplete Solutions
22+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/high-scores/example.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class HighScores(object):
2+
def __init__(self, scores):
3+
self.scores = scores
4+
5+
def latest(self):
6+
return self.scores[-1]
7+
8+
def highest(self):
9+
return max(self.scores)
10+
11+
def top(self):
12+
return sorted(self.scores, reverse=True)[:3]
13+
14+
def report(self):
15+
difference = self.highest() - self.latest()
16+
result_qualifier = (
17+
"" if difference <= 0 else "{} short of ".format(difference)
18+
)
19+
return "Your latest score was {}. That's {}your personal best!".format(
20+
self.latest(), result_qualifier
21+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class HighScores(object):
2+
def __init__(self, scores):
3+
pass
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import unittest
2+
3+
from high_scores import HighScores
4+
5+
6+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
7+
8+
9+
class HighScoreTest(unittest.TestCase):
10+
def test_list_of_scores(self):
11+
scores = [30, 50, 20, 70]
12+
expected = [30, 50, 20, 70]
13+
self.assertEqual(HighScores(scores).scores, expected)
14+
15+
def test_latest_score(self):
16+
scores = [100, 0, 90, 30]
17+
expected = 30
18+
self.assertEqual(HighScores(scores).latest(), expected)
19+
20+
def test_highest_score(self):
21+
scores = [40, 100, 70]
22+
expected = 100
23+
self.assertEqual(HighScores(scores).highest(), expected)
24+
25+
def test_top_3_scores(self):
26+
scores = [50, 30, 10]
27+
expected = [50, 30, 10]
28+
self.assertEqual(HighScores(scores).top(), expected)
29+
30+
def test_personal_bests_highest_to_lowest(self):
31+
scores = [20, 10, 30]
32+
expected = [30, 20, 10]
33+
self.assertEqual(HighScores(scores).top(), expected)
34+
35+
def test_personal_bests_when_there_is_a_tie(self):
36+
scores = [40, 20, 40, 30]
37+
expected = [40, 40, 30]
38+
self.assertEqual(HighScores(scores).top(), expected)
39+
40+
def test_personal_bests_when_there_are_less_than_3(self):
41+
scores = [30, 70]
42+
expected = [70, 30]
43+
self.assertEqual(HighScores(scores).top(), expected)
44+
45+
def test_personal_bests_when_there_is_only_one(self):
46+
scores = [40]
47+
expected = [40]
48+
self.assertEqual(HighScores(scores).top(), expected)
49+
50+
def test_personal_bests_from_a_long_list(self):
51+
scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
52+
expected = [100, 90, 70]
53+
self.assertEqual(HighScores(scores).top(), expected)
54+
55+
def test_message_for_new_personal_best(self):
56+
scores = [20, 40, 0, 30, 70]
57+
expected = "Your latest score was 70. That's your personal best!"
58+
self.assertEqual(HighScores(scores).report(), expected)
59+
60+
def test_message_when_latest_score_is_not_the_highest_score(self):
61+
scores = [20, 100, 0, 30, 70]
62+
expected = (
63+
"Your latest score was 70. That's 30 short of your personal best!"
64+
)
65+
self.assertEqual(HighScores(scores).report(), expected)
66+
67+
def test_message_for_repeated_personal_best(self):
68+
scores = [20, 70, 50, 70, 30]
69+
expected = (
70+
"Your latest score was 30. That's 40 short of your personal best!"
71+
)
72+
self.assertEqual(HighScores(scores).report(), expected)
73+
74+
75+
if __name__ == "__main__":
76+
unittest.main()

0 commit comments

Comments
 (0)