Skip to content

Commit 409b761

Browse files
statkutegodlygeek
authored andcommitted
tutorial: Fixes for Exercise 3
The PR fixes the issues with the exercise 3 from the tutorial: - There was an inconsistency between the code in the solution vs the exercise, as the exercise used `@classmethod` and the solution did not. Now that's been made consistent - The 2nd solution did not lower the memory usage. Not sure why it was added there - the PR removes it - An edit to the solution for consistency to utilise the same global variables instead of magic numbers Signed-off-by: Gintare Statkute <[email protected]>
1 parent 98cdceb commit 409b761

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

docs/tutorials/3.rst

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ There are many different approaches to fix this memory issue - here are a few of
132132
self.factorial_plus = functools.cache(self._uncached_factorial_plus)
133133

134134
def _uncached_factorial_plus(self, n: int) -> int:
135-
return n * self.factorial_plus(n - 1) + self.inc if n else 1 + self.inc
136-
135+
return n * self.factorial_plus(n - 1) + self.inc if n > 1 else 1 + self.inc
137136

138137
def generate_factorial_plus_last_digit(plus_range: int, factorial_range: int):
139138
for i in range(plus_range):
@@ -144,21 +143,7 @@ There are many different approaches to fix this memory issue - here are a few of
144143
Full code solution `here
145144
<https://github.com/bloomberg/memray/blob/main/docs/tutorials/solutions/exercise_3/lru_cache.py>`_.
146145

147-
2. Or you can use a ``classmethod`` for the cache instead of an instance method::
148-
149-
class Algorithms:
150-
def __init__(self, inc: int):
151-
self.inc = inc
152-
153-
def factorial_plus(self, n: int) -> int:
154-
return self.factorial_plus_impl(n, self.inc)
155-
156-
@classmethod
157-
@functools.cache
158-
def factorial_plus_impl(cls, n: int, inc: int) -> int:
159-
return n * cls.factorial_plus_impl(n - 1, inc) + inc if n > 1 else 1 + inc
160-
161-
3. Another approach would be setting a maximum size for the cache. We can do
146+
2. Another approach would be setting a maximum size for the cache. We can do
162147
that by passing an argument to ``@lru_cache`` decorator directly.
163148

164149
.. note::
@@ -175,7 +160,7 @@ There are many different approaches to fix this memory issue - here are a few of
175160
176161
``maxsize=`` here sets the maximum number of values stored in the cache.
177162

178-
4. Finally, we can periodically manually invoke the cleanup of the cache. This can be done by calling
163+
3. Finally, we can periodically manually invoke the cleanup of the cache. This can be done by calling
179164
``Algorithms.factorial_plus.cache_clear()``
180165

181166
.. raw:: html

docs/tutorials/exercise_3/lru_cache.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ class Algorithms:
1313
def __init__(self, inc: int):
1414
self.inc = inc
1515

16+
@functools.cache # pylint: disable=W1518
1617
def factorial_plus(self, n: int) -> int:
17-
return self.factorial_plus_impl(n, self.inc)
18-
19-
@classmethod
20-
@functools.cache
21-
def factorial_plus_impl(cls, n: int, inc: int) -> int:
22-
return n * cls.factorial_plus_impl(n - 1, inc) + inc if n > 1 else 1 + inc
18+
return n * self.factorial_plus(n - 1) + self.inc if n > 1 else 1 + self.inc
2319

2420

2521
def generate_factorial_plus_last_digit(plus_range: int, factorial_range: int):

docs/tutorials/solutions/exercise_3/lru_cache.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import functools
22
from collections import Counter
33

4+
# DO NOT CHANGE
5+
FIRST_COUNTER_RANGE = 500
6+
SECOND_COUNTER_RANGE = 1000
7+
# DO NOT CHANGE
8+
49

510
class Algorithms:
611
def __init__(self, inc: int):
@@ -19,8 +24,12 @@ def generate_factorial_plus_last_digit(plus_range: int, factorial_range: int):
1924

2025

2126
def compare_counts_different_factorials():
22-
counts_500 = Counter(generate_factorial_plus_last_digit(500, 500))
23-
counts_1000 = Counter(generate_factorial_plus_last_digit(1000, 1000))
27+
counts_500 = Counter(
28+
generate_factorial_plus_last_digit(FIRST_COUNTER_RANGE, FIRST_COUNTER_RANGE)
29+
)
30+
counts_1000 = Counter(
31+
generate_factorial_plus_last_digit(SECOND_COUNTER_RANGE, SECOND_COUNTER_RANGE)
32+
)
2433
print(counts_500.most_common())
2534
print(counts_1000.most_common())
2635

0 commit comments

Comments
 (0)