Glasgow | Prati Amalden | Module Complexity | Sprint 2 | Improve with caching#66
Glasgow | Prati Amalden | Module Complexity | Sprint 2 | Improve with caching#66PratiAmalden wants to merge 1 commit intoCodeYourFuture:mainfrom
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.
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.
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.