Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
high-scores: Implement new exercise (#1572)
* high-scores: Implement new exercise * Adapt to default flake8 line length of 80 * Switch properties to methods * Add topics
- Loading branch information
1 parent
c61caaa
commit a57699a
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class HighScores(object): | ||
def __init__(self, scores): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |