Skip to content

Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Improve code with caches#130

Open
HassanOHOsman wants to merge 21 commits intoCodeYourFuture:mainfrom
HassanOHOsman:feature/improve-code-with-cashes
Open

Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Improve code with caches#130
HassanOHOsman wants to merge 21 commits intoCodeYourFuture:mainfrom
HassanOHOsman:feature/improve-code-with-cashes

Conversation

@HassanOHOsman
Copy link

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

In this pull request, I introduced caching (memoisation) to Fibonacci and Making Change directories to make the implementation more efficient.

@HassanOHOsman HassanOHOsman 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. labels Feb 28, 2026
Comment on lines 11 to 28
@@ -29,4 +26,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

@cjyuan cjyuan Mar 2, 2026

Choose a reason for hiding this comment

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

Array creation is a relatively costly operation.

From line 26, 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]
...
[]

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-arrays at line 26 (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
Author

Choose a reason for hiding this comment

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

Done.

@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
@HassanOHOsman HassanOHOsman 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 Mar 3, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

Changes look good. Well done.

@@ -1,32 +1,38 @@
from typing import List

coins = [200, 100, 50, 20, 10, 5, 2, 1]
Copy link

Choose a reason for hiding this comment

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

  • If this is to be used as a constant, best practice is to name it with all uppercase letters.

  • We could also keep the coins array in the parameter. Arrays are pass by reference to a function so the overhead is negligible.

Comment on lines +15 to 23
if key in cache:
return cache[key]

if total == 0:
return 1


if coin_index == len(coins):
return 0
Copy link

Choose a reason for hiding this comment

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

Note: Even though key construction and dictionary lookup are O(1) operations, they are still relatively costly than some simple numerical computation or comparisons (code on lines 18 and 22).

This is unrelated to cache, but if you swap the code on lines 18-23 with the code on lines 13-16, you can probably notice some slight improvement in performance.

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 3, 2026
@cjyuan cjyuan added the Complete Volunteer to add when work is complete and all review comments have been addressed. label Mar 3, 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