Skip to content

NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | improve with caches#171

Open
TzeMingHo wants to merge 4 commits into
CodeYourFuture:mainfrom
TzeMingHo:coursework/improve_with_caches
Open

NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | improve with caches#171
TzeMingHo wants to merge 4 commits into
CodeYourFuture:mainfrom
TzeMingHo:coursework/improve_with_caches

Conversation

@TzeMingHo

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

In the making_change exercise, I have to discard the for loop and the while loop because it was difficult for me to understand what was happening. By implementing the take or leave the coin logic with coin_index, it explores the combinations with different starting coins. When total hits 0, it finds a way.

@TzeMingHo TzeMingHo 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 Jun 19, 2026
Comment on lines +1 to +9
def fibonacci(n, cache=None):
if cache == None:
cache={0:0, 1:1}
if n in cache:
return cache[n]
else:
value = fibonacci(n - 1, cache) + fibonacci(n - 2, cache)
cache[n] = value
return value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you research different approaches to make the cache reusable across multiple function calls (instead of repopulating it from the beginning)? Ideally, the approach does not involve using any global variable.

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.

I tried to use attributes. Is it a better way?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your implementation looks good.

Note: there is a much faster way to figure out how many ways we could make change when there is only 1 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.

So, when coin_index hits 7, which leads to coin value 1, we can simply return 1, right? No matter what the total is left, with coin value 1, it always gets 1 way.

@cjyuan cjyuan Jun 22, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct.

I see that you have also figured out how to compute number of ways to make change when there is one coin left using %. Nice!

@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 Jun 21, 2026
@TzeMingHo TzeMingHo 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 Jun 22, 2026
@cjyuan

cjyuan commented Jun 22, 2026

Copy link
Copy Markdown

Changes look great. Well done.

@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 Jun 22, 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