Skip to content

London | 26-JUL-SDC |Ping Wang | Sprint 2 | Improve caches CYF-1182 - #232

Open
pathywang wants to merge 4 commits into
CodeYourFuture:mainfrom
pathywang:improve-caches
Open

pathywang wants to merge 4 commits into
CodeYourFuture:mainfrom
pathywang:improve-caches

Conversation

@pathywang

@pathywang pathywang commented Oct 4, 2026 •

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

Task code

CYF-1182

Changelist

. Updated the Fibonacci function to use the existing cache dictionary, avoiding repeated calculations and
improving performance through memoisation.

  • Optimized ways_to_make_change by replacing coin-list slicing with a coin index.
  • Updated memoization to use (total, coin_index) as the cache key, avoiding repeated tuple creation.
  • Used recursive cases to count combinations by either skipping the current coin or using it again.

@github-actions

This comment has been minimized.

@pathywang pathywang changed the title Lond | 26-JUL-SDC |Ping Wang | Sprint 2 | Improve caches CYF-1182 London | 26-JUL-SDC |Ping Wang | Sprint 2 | Improve caches CYF-1182 Oct 4, 2026
@github-actions

This comment has been minimized.

@pathywang pathywang added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 4, 2026
Comment on lines +1 to +4
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.

This variable is not used anywhere in this file.

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.

Thank, i deleted it now while cache variable is never used in fibronacci function

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wouldn't deleting it beat the purpose of this exercise?

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.

Thank you for pointing out. I've kept the original Fibonacci implementation and updated it to use the existing cache dictionary. How do you think?

Comment on lines 18 to 37
@@ -29,4 +35,7 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
ways += intermediate
count_of_coin += 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 27 and 35, 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 35 (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.

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.

Thanks for the feedback! I updated the implementation to use a coin index instead of creating sliced lists. The memoization cache now uses (total, coin_index) as its key, avoiding repeated tuple and list creation while preserving the logic for counting combinations.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sounds good.

I don't see any new commits on GitHub yet.

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

Do apologize, i forgot to commit and push. i just did it

@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 9, 2026
@pathywang pathywang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 10, 2026
@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 10, 2026
@pathywang pathywang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 10, 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 1 to -11
@@ -8,34 +8,32 @@ def ways_to_make_change(total: int) -> int:

For instance, there are two ways to make a value of 3: with 3x 1 coins, or with 1x 1 coin and 1x 2 coin.
"""
return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 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.

Why use tuple instead of list?

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.

Thank you for asking.I changed the coin denominations to a tuple because they are fixed and do not need to be modified. The main optimization was replacing list slicing with a coin index, avoiding the creation of new lists during recursive calls. The cache now uses (total, coin_index) as its key.

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