From 89bb17a15563b1bfb47e6f7659c5c82d8d20021f Mon Sep 17 00:00:00 2001 From: Pavlin Angelov Date: Fri, 3 May 2019 21:30:18 +0300 Subject: [PATCH] Simplify High-scores exercise. Replace Class with functions (#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 * Update exercises/matrix/README.md Co-Authored-By: simmol * Regenerate README --- .gitignore | 1 + exercises/high-scores/.meta/HINTS.md | 8 ++++---- exercises/high-scores/README.md | 8 ++++---- exercises/high-scores/example.py | 16 +++++++--------- exercises/high-scores/high_scores.py | 13 ++++++++++--- exercises/high-scores/high_scores_test.py | 20 ++++++++------------ 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index edce8452..6f97f357 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ bin/configlet bin/configlet.exe .idea/ .cache +.pytest_cache __pycache__ diff --git a/exercises/high-scores/.meta/HINTS.md b/exercises/high-scores/.meta/HINTS.md index d1d5af11..b1de85b1 100644 --- a/exercises/high-scores/.meta/HINTS.md +++ b/exercises/high-scores/.meta/HINTS.md @@ -1,5 +1,5 @@ -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!_ +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. -- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation. -- [**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. -- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation. +- [**Data Structures (Python 3 Documentation Tutorial)**](https://docs.python.org/3/tutorial/datastructures.html) +- [**Lists and Tuples in Python (Real Python)**](https://realpython.com/python-lists-tuples/) +- [**Python Lists (Google for Education)**](https://developers.google.com/edu/python/lists) diff --git a/exercises/high-scores/README.md b/exercises/high-scores/README.md index 6b08db6e..fa7934d4 100644 --- a/exercises/high-scores/README.md +++ b/exercises/high-scores/README.md @@ -4,11 +4,11 @@ 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 and the three highest scores. -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!_ +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. -- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation. -- [**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. -- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation. +- [**Data Structures (Python 3 Documentation Tutorial)**](https://docs.python.org/3/tutorial/datastructures.html) +- [**Lists and Tuples in Python (Real Python)**](https://realpython.com/python-lists-tuples/) +- [**Python Lists (Google for Education)**](https://developers.google.com/edu/python/lists) diff --git a/exercises/high-scores/example.py b/exercises/high-scores/example.py index 0c77d73a..fd168ffc 100644 --- a/exercises/high-scores/example.py +++ b/exercises/high-scores/example.py @@ -1,12 +1,10 @@ -class HighScores(object): - def __init__(self, scores): - self.scores = scores +def latest(scores): + return scores[-1] - def latest(self): - return self.scores[-1] - def personal_best(self): - return max(self.scores) +def personal_best(scores): + return max(scores) - def personal_top_three(self): - return sorted(self.scores, reverse=True)[:3] + +def personal_top_three(scores): + return sorted(scores, reverse=True)[:3] diff --git a/exercises/high-scores/high_scores.py b/exercises/high-scores/high_scores.py index 3bfba4d0..0d24cdb1 100644 --- a/exercises/high-scores/high_scores.py +++ b/exercises/high-scores/high_scores.py @@ -1,3 +1,10 @@ -class HighScores(object): - def __init__(self, scores): - pass +def latest(scores): + pass + + +def personal_best(scores): + pass + + +def personal_top_three(scores): + pass diff --git a/exercises/high-scores/high_scores_test.py b/exercises/high-scores/high_scores_test.py index 17d69c0e..40bd76c5 100644 --- a/exercises/high-scores/high_scores_test.py +++ b/exercises/high-scores/high_scores_test.py @@ -1,51 +1,47 @@ import unittest -from high_scores import HighScores +from high_scores import latest, personal_best, personal_top_three # Tests adapted from `problem-specifications//canonical-data.json` @ v4.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) + self.assertEqual(latest(scores), expected) def test_personal_best(self): scores = [40, 100, 70] expected = 100 - self.assertEqual(HighScores(scores).personal_best(), expected) + self.assertEqual(personal_best(scores), expected) def test_personal_top_three_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).personal_top_three(), expected) + self.assertEqual(personal_top_three(scores), expected) def test_personal_top_three_highest_to_lowest(self): scores = [20, 10, 30] expected = [30, 20, 10] - self.assertEqual(HighScores(scores).personal_top_three(), expected) + self.assertEqual(personal_top_three(scores), expected) def test_personal_top_three_when_there_is_a_tie(self): scores = [40, 20, 40, 30] expected = [40, 40, 30] - self.assertEqual(HighScores(scores).personal_top_three(), expected) + self.assertEqual(personal_top_three(scores), expected) def test_personal_top_three_when_there_are_less_than_3(self): scores = [30, 70] expected = [70, 30] - self.assertEqual(HighScores(scores).personal_top_three(), expected) + self.assertEqual(personal_top_three(scores), expected) def test_personal_top_three_when_there_is_only_one(self): scores = [40] expected = [40] - self.assertEqual(HighScores(scores).personal_top_three(), expected) + self.assertEqual(personal_top_three(scores), expected) if __name__ == "__main__":