Skip to content

Glasgow | 25-SDC-Nov | Nataliia Volkova | Sprint 2 | improve_with_caches#106

Open
Nataliia74 wants to merge 3 commits intoCodeYourFuture:mainfrom
Nataliia74:fibonacci
Open

Glasgow | 25-SDC-Nov | Nataliia Volkova | Sprint 2 | improve_with_caches#106
Nataliia74 wants to merge 3 commits intoCodeYourFuture:mainfrom
Nataliia74:fibonacci

Conversation

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

Improve code with caches 11

@Nataliia74 Nataliia74 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 16, 2026
Comment on lines +27 to +30
if total == 1 or len(coins) == 1:
result = 1
cache[key] = result
return result
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When total == 1 or len(coins) == 1 is true, the result may not be 1 if the last coin is not 1 (e.g. the initial list of coins given is different)

On the other hand, when len(coins) == 1, there is simple way to determine if the current total (any value) can be made by the coin value.

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.

Hello CJ Yuan, I understood that I can have 2 possible results, not just 1. So, the best founded solution is not to use recursion, but simple math to check if the total is divisible by that left type coin.

Copy link
Copy Markdown

@cjyuan cjyuan Apr 21, 2026

Choose a reason for hiding this comment

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

What do you mean by 2 possible results? 0 or 1?

Yes, this approach:

simple math to check if the total is divisible by that left type coin

Copy link
Copy Markdown
Author

@Nataliia74 Nataliia74 Apr 22, 2026

Choose a reason for hiding this comment

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

Hello CJ Yuan, I mean 0 or 1. In this case, a better approach is to use math.

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 creation is a relatively costly operation.

From line 41, 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 41 (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.

Eliminated slicing, which is costly and creates a new array every recursion, and also many duplicates; the tuple conversion was costly as well. Changed by exchanging the tuple at the index an array.

@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 22, 2026
@Nataliia74 Nataliia74 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 Apr 21, 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.

Changes look good.

Comment on lines +3 to +4
cache = {}
coins = [1, 2, 5, 10, 20, 50, 100, 200]
Copy link
Copy Markdown

@cjyuan cjyuan Apr 23, 2026

Choose a reason for hiding this comment

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

Should you need to avoid introducing variables in the global scope, you can consider:

  • Declare them in ways_to_make_change()
  • Define ways_to_make_change_helper() as an inner function of ways_to_make_change(). Inner function can access the variables in the outer scope.

@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 Apr 23, 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