Skip to content

Comments

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

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

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

Conversation

@Nataliia74
Copy link

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

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

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.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants