Skip to content

London | 26-Jul-SDC | Roman Sanaye | Sprint 2 | Improve Code with Caches - #249

Open
RomanSanaye wants to merge 4 commits into
CodeYourFuture:mainfrom
RomanSanaye:improve-code-with-cache
Open

RomanSanaye wants to merge 4 commits into
CodeYourFuture:mainfrom
RomanSanaye:improve-code-with-cache

Conversation

@RomanSanaye

@RomanSanaye RomanSanaye commented Oct 13, 2026 •

Copy link
Copy Markdown

Learners, PR Template

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

Task code

CYF-1182

Changelist

Implemented caching (memoization) for the ways_to_make_change solution.

  • Added a cache dictionary to store previously calculated results.
  • Used (total, tuple(coins)) as the cache key.
  • Added cache lookups before performing recursive calculations.
  • Stored computed results in the cache for future reuse.
  • Ran the provided test suite and confirmed that all tests pass.

@github-actions

This comment has been minimized.

@RomanSanaye RomanSanaye added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 13, 2026
Comment on lines +1 to +3
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
# Initialize the cache dictionary
cache = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The cached values of this function could be reused between function calls. You could consider using function attribute instead.

Change is optional.

Comment on lines +20 to +38
@@ -26,7 +35,10 @@ 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
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 30 and 38, 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 38 (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 Oct 14, 2026
@RomanSanaye

Copy link
Copy Markdown
Author

Hi @cjyuan ,
Thanks for the suggestion! Using a function attribute for memoization is a clean way to persist the cache across multiple calls without using global variables. I've updated it!

@RomanSanaye RomanSanaye added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 15, 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.

Looks good.

@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 Oct 15, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants