Skip to content

Commit 89bb17a

Browse files
simmolcmccandless
authored andcommitted
Simplify High-scores exercise. Replace Class with functions (exercism#1764)
* Simplify High-scores exercise. Replace Class with functions Exercise text edited. Tests made to expect functions. One test removed, cause was no longer valid. Make new example.py using functions. Add the functions as starting point in the exercise file. (This also make the tests not complain of missing imports and such.) Added __init__.py to the package for the people that still run 2.7+ Added pytest cache to gitignore file * Fix style problems * Made requested changes in PR Remove __init__.py Remove content of HINTS.md Generate the README.md Remove the comments in stub file * Add new hints to high-score Added hints for some docs around list and tuples - thanks @BethanyG for that Moved original hints for classes in matrix ( cause is the next exercise for now which use class) slightly edited to not refer to high-score Regenerate both README.md * Remove changes to matrix from this PR * Update exercises/high-scores/.meta/HINTS.md Co-Authored-By: simmol <simmol@users.noreply.github.com> * Update exercises/matrix/README.md Co-Authored-By: simmol <simmol@users.noreply.github.com> * Regenerate README
1 parent 839e3fe commit 89bb17a

6 files changed

Lines changed: 34 additions & 32 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ bin/configlet
88
bin/configlet.exe
99
.idea/
1010
.cache
11+
.pytest_cache
1112
__pycache__
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
In this exercise you're going to create a **class** and use lists to organize scores. _Don't worry, it's not as complicated as you think!_
1+
In this exercise, you're going to use and manipulate lists. Python lists are very versatile, and you'll find yourself using them again and again in problems both simple and complex.
22

3-
- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation.
4-
- [**How to Define a Class in Python**](https://realpython.com/python3-object-oriented-programming/#how-to-define-a-class-in-python) from the Real Python website.
5-
- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation.
3+
- [**Data Structures (Python 3 Documentation Tutorial)**](https://docs.python.org/3/tutorial/datastructures.html)
4+
- [**Lists and Tuples in Python (Real Python)**](https://realpython.com/python-lists-tuples/)
5+
- [**Python Lists (Google for Education)**](https://developers.google.com/edu/python/lists)

exercises/high-scores/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Manage a game player's High Score list.
44

55
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 and the three highest scores.
66

7-
In this exercise you're going to create a **class** and use lists to organize scores. _Don't worry, it's not as complicated as you think!_
7+
In this exercise, you're going to use and manipulate lists. Python lists are very versatile, and you'll find yourself using them again and again in problems both simple and complex.
88

9-
- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation.
10-
- [**How to Define a Class in Python**](https://realpython.com/python3-object-oriented-programming/#how-to-define-a-class-in-python) from the Real Python website.
11-
- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation.
9+
- [**Data Structures (Python 3 Documentation Tutorial)**](https://docs.python.org/3/tutorial/datastructures.html)
10+
- [**Lists and Tuples in Python (Real Python)**](https://realpython.com/python-lists-tuples/)
11+
- [**Python Lists (Google for Education)**](https://developers.google.com/edu/python/lists)
1212

1313

1414

exercises/high-scores/example.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
class HighScores(object):
2-
def __init__(self, scores):
3-
self.scores = scores
1+
def latest(scores):
2+
return scores[-1]
43

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

8-
def personal_best(self):
9-
return max(self.scores)
5+
def personal_best(scores):
6+
return max(scores)
107

11-
def personal_top_three(self):
12-
return sorted(self.scores, reverse=True)[:3]
8+
9+
def personal_top_three(scores):
10+
return sorted(scores, reverse=True)[:3]
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
class HighScores(object):
2-
def __init__(self, scores):
3-
pass
1+
def latest(scores):
2+
pass
3+
4+
5+
def personal_best(scores):
6+
pass
7+
8+
9+
def personal_top_three(scores):
10+
pass

exercises/high-scores/high_scores_test.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
import unittest
22

3-
from high_scores import HighScores
3+
from high_scores import latest, personal_best, personal_top_three
44

55

66
# Tests adapted from `problem-specifications//canonical-data.json` @ v4.0.0
77

88

99
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)
1410

1511
def test_latest_score(self):
1612
scores = [100, 0, 90, 30]
1713
expected = 30
18-
self.assertEqual(HighScores(scores).latest(), expected)
14+
self.assertEqual(latest(scores), expected)
1915

2016
def test_personal_best(self):
2117
scores = [40, 100, 70]
2218
expected = 100
23-
self.assertEqual(HighScores(scores).personal_best(), expected)
19+
self.assertEqual(personal_best(scores), expected)
2420

2521
def test_personal_top_three_from_a_long_list(self):
2622
scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
2723
expected = [100, 90, 70]
28-
self.assertEqual(HighScores(scores).personal_top_three(), expected)
24+
self.assertEqual(personal_top_three(scores), expected)
2925

3026
def test_personal_top_three_highest_to_lowest(self):
3127
scores = [20, 10, 30]
3228
expected = [30, 20, 10]
33-
self.assertEqual(HighScores(scores).personal_top_three(), expected)
29+
self.assertEqual(personal_top_three(scores), expected)
3430

3531
def test_personal_top_three_when_there_is_a_tie(self):
3632
scores = [40, 20, 40, 30]
3733
expected = [40, 40, 30]
38-
self.assertEqual(HighScores(scores).personal_top_three(), expected)
34+
self.assertEqual(personal_top_three(scores), expected)
3935

4036
def test_personal_top_three_when_there_are_less_than_3(self):
4137
scores = [30, 70]
4238
expected = [70, 30]
43-
self.assertEqual(HighScores(scores).personal_top_three(), expected)
39+
self.assertEqual(personal_top_three(scores), expected)
4440

4541
def test_personal_top_three_when_there_is_only_one(self):
4642
scores = [40]
4743
expected = [40]
48-
self.assertEqual(HighScores(scores).personal_top_three(), expected)
44+
self.assertEqual(personal_top_three(scores), expected)
4945

5046

5147
if __name__ == "__main__":

0 commit comments

Comments
 (0)