Skip to content

NW| 26-Jul-SDC | Ahmad Hmedan | Sprint 2 | Improve code with caches - #235

Open
AhmadHmedann wants to merge 5 commits into
CodeYourFuture:mainfrom
AhmadHmedann:improve_with_caches
Open

AhmadHmedann wants to merge 5 commits into
CodeYourFuture:mainfrom
AhmadHmedann:improve_with_caches

Conversation

@AhmadHmedann

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

@AhmadHmedann AhmadHmedann 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 +20 to +37
@@ -26,7 +32,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:])
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 27 and 36, 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 36 (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.

Thank you for the feedback. I hope this addresses your points. I've updated the code to reuse the original list instead of creating new lists and tuples in each recursive call.

@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
@AhmadHmedann AhmadHmedann 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 11, 2026
@AhmadHmedann
AhmadHmedann requested a review from cjyuan October 11, 2026 14:09
@cjyuan

cjyuan commented Oct 11, 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 Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 11, 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