Skip to content

London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with caches#88

Open
RihannaP wants to merge 3 commits intoCodeYourFuture:mainfrom
RihannaP:sprint2-improve-with-caches
Open

London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with caches#88
RihannaP wants to merge 3 commits intoCodeYourFuture:mainfrom
RihannaP:sprint2-improve-with-caches

Conversation

@RihannaP
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

Changelist

I tried to improve the performance by :

  • adding memo dictionary
  • adding cache using tuple keys and solve problem with math (not loop)

Questions

No question

@RihannaP RihannaP 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 Jan 30, 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 Feb 15, 2026
@RihannaP RihannaP 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 Mar 3, 2026
Copy link
Copy Markdown

@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.

Code is good.

Thanks for updating the label for me.

Comment on lines +21 to +35
key = (total, suffix_id)
if key in cache:
return cache[key]

coins = COIN_SUFFIXES[suffix_id]

# Keep behavior close to original, but fix the standard base case:
if total == 0:
return 1
if len(coins) == 0:
return 0

if len(coins) == 1:
cache[key] = 1 if (total % coins[0] == 0) else 0
return cache[key]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note: Even though tuple construction and dictionary lookup (lines 21 and 22) are O(1) operations,
they are still relatively costly than some simple numerical computation and comparisons (lines 28, 30, 33, 34).

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


Good job in figuring out a quicker way to compute number of ways when there is only one coin left to consider.

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.

Agreed. Checking that before doing cache lookup avoids unnecessary tuple creation and dictionary access

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