Skip to content

London | 26-SDC-March | Zobeir Rigi | Sprint 2 | Improve code with caches - #209

Open
Zobeir-Rigi wants to merge 4 commits into
CodeYourFuture:mainfrom
Zobeir-Rigi:improve-code-with-caches
Open

London | 26-SDC-March | Zobeir Rigi | Sprint 2 | Improve code with caches#209
Zobeir-Rigi wants to merge 4 commits into
CodeYourFuture:mainfrom
Zobeir-Rigi:improve-code-with-caches

Conversation

@Zobeir-Rigi

Copy link
Copy Markdown
  • 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

I originally completed all the Sprint 2 exercises in one PR, but the validation expected separate PRs for each issue. This PR contains only the "Improve code with caches" exercise.

Added memoisation/caching to improve the performance of:

  • fibonacci
  • making_change

All provided tests pass.

@Zobeir-Rigi Zobeir-Rigi 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 Jul 6, 2026
@@ -1,4 +1,10 @@
cache = {0: 0, 1: 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.

Could you look into an approach that doesn't require keeping the cache in the global scope?

@Zobeir-Rigi Zobeir-Rigi Jul 14, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Could you look into an approach that doesn't require keeping the cache in the global scope?

I updated the implementation so the cache is no longer stored in the global scope.

Comment on lines +39 to +42
ways += ways_to_make_change_helper(
total - total_from_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.

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-arrays at line 41 (e.g. use another cache), and
  • create key (on line 19) 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.

Copy link
Copy Markdown
Author

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.

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-arrays at line 41 (e.g. use another cache), and

  • create key (on line 19) 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.

Thanks, I refactored the solution to avoid repeatedly creating sublists and tuples. The implementation now uses a fixed coin list and passes a start index through the recursive calls, which allows the cache key to be (total, start_index) and reduces unnecessary allocations.

@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 Jul 9, 2026
@cjyuan

cjyuan commented Jul 15, 2026

Copy link
Copy Markdown

Changes look good.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jul 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. Module-Complexity The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants