Skip to content

London | November 25 | Donara Blanc | Sprint 2 | Improve with caches#132

Open
donarbl wants to merge 1 commit intoCodeYourFuture:mainfrom
donarbl:sprint2-improve-with-caches
Open

London | November 25 | Donara Blanc | Sprint 2 | Improve with caches#132
donarbl wants to merge 1 commit intoCodeYourFuture:mainfrom
donarbl:sprint2-improve-with-caches

Conversation

@donarbl
Copy link

@donarbl donarbl commented Feb 28, 2026

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

both functions now avoid redundant recursive calls

@donarbl donarbl added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Feb 28, 2026
Comment on lines 10 to +26
@@ -26,7 +19,12 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
if total_from_coins == total:
ways += 1
else:
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
intermediate = ways_to_make_change_helper(
total - total_from_coins,
coins=coins[coin_index + 1:],
cache=cache
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuple or array creation are relatively costly operations.

From lines 5 and 24, we know coins can only be one of the following 9 tuples:

(200, 100, 50, 20, 10, 5, 2, 1)
(100, 50, 20, 10, 5, 2, 1)
(50, 20, 10, 5, 2, 1)
...
(1)
()

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-tuples at line 24 (e.g. use another cache), and
  • create key as (total, a_unique_integer_identifying_the_subtuple) instead of as (total, tuple of coins)
    • There are only a small number of different sub-tuples. We can easily assign each sub-tuple a unique integer.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Complexity The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants