-
-
Notifications
You must be signed in to change notification settings - Fork 14
Glasgow | Prati Amalden | Module Complexity | Sprint 2 | Improve with caching #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Your PR description contained template fields which weren't filled in. Check you've ticked everything in the self checklist, and that any sections which prompt you to fill in an answer are either filled in or removed. If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
|
Your PR description contained template fields which weren't filled in. Check you've ticked everything in the self checklist, and that any sections which prompt you to fill in an answer are either filled in or removed. If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
| count_of_coin = 0 | ||
|
|
||
| while coin * count_of_coin <= total: | ||
| total_from_coins = coin * count_of_coin | ||
| intermediate = ways_to_make_change_helper(total - total_from_coins, rest) | ||
| ways += intermediate | ||
| count_of_coin += 1 | ||
|
|
||
| _change_cache[key] = ways |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job in identifying that we could get rid of the outer for loop.
| key = (total, coins) | ||
| if key in _change_cache: | ||
| return _change_cache[key] | ||
|
|
||
| coin = coins[0] | ||
| rest = coins[1:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From line 29, we could identify coins can only be one of the following 9 tuples:
(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-tuples at line 29 (e.g. use another cache), and
- create
keyas (total, an_integer_that_map_to_the_subtuple) instead of as (total, tuple of coins)
I don't think this exercise expects trainees to optimize the code to this level. So change is optional.
No description provided.