Skip to content

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Add caching to improve performance#144

Open
aydaeslami wants to merge 2 commits into
CodeYourFuture:mainfrom
aydaeslami:Improve-code-with-caches
Open

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 2 | Add caching to improve performance#144
aydaeslami wants to merge 2 commits into
CodeYourFuture:mainfrom
aydaeslami:Improve-code-with-caches

Conversation

@aydaeslami

Copy link
Copy Markdown

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

Added caching (memoisation) to avoid repeated calculations and improve performance of recursive functions.

@aydaeslami aydaeslami added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 2 Assigned during Sprint 2 of this module labels Mar 1, 2026
Comment on lines +10 to +33
@@ -26,7 +27,11 @@ 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:])
ways += intermediate
ways += ways_to_make_change_helper(
total - total_from_coins,
coins=coins[coin_index + 1:]
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Array and tuple creations are relatively costly operations.

From lines 6 and 32, we know coins can only be one of the following 9 arrays:

[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-arrays at line 32 (e.g. use another cache), and
  • create key as (total, a_unique_integer_identifying_the_subarray) instead of as (total, tuple of coins)
    • There are only a small number of different subarrays. We can easily assign each subarray 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
@aydaeslami

Copy link
Copy Markdown
Author

Thanks a million @cjyuan for your suggestion, I updated the solution to use start_index instead of creating coin sub-arrays, and changed the cache key to total, start_index to avoid unnecessary array and tuple creation.
Please let me know if there is anything that I should change.

@aydaeslami aydaeslami added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 2, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Changes look good.

Comment on lines -9 to +11
def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
key = (total, tuple(coins))
def ways_to_make_change_helper(total: int, start_index: int) -> int:
key = (total, start_index)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why not pass the coin list through the parameter (in addition to passing start_index) so that the function can be reused for different coin list?

When we pass a list to a function, we are only passing its reference -- the cost is negligible.

ways = 0
for coin_index in range(len(coins)):
coin = coins[coin_index]
for coin_index in range(start_index, len(COINS)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This loop is actually redundant. Subsequent coins will be considered in the recursive function calls anyway.

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

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants