London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with caches#88
London | 25-SDC-July | Rihanna Poursoltani | Sprint 2 | Improve code with caches#88RihannaP wants to merge 3 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code is good.
Thanks for updating the label for me.
| key = (total, suffix_id) | ||
| if key in cache: | ||
| return cache[key] | ||
|
|
||
| coins = COIN_SUFFIXES[suffix_id] | ||
|
|
||
| # Keep behavior close to original, but fix the standard base case: | ||
| if total == 0: | ||
| return 1 | ||
| if len(coins) == 0: | ||
| return 0 | ||
|
|
||
| if len(coins) == 1: | ||
| cache[key] = 1 if (total % coins[0] == 0) else 0 | ||
| return cache[key] |
There was a problem hiding this comment.
Note: Even though tuple construction and dictionary lookup (lines 21 and 22) are O(1) operations,
they are still relatively costly than some simple numerical computation and comparisons (lines 28, 30, 33, 34).
This is unrelated to cache, but if you swap the code on lines 28-33 with the code on lines 21-23,
you can probably notice some slight improvement in performance.
Good job in figuring out a quicker way to compute number of ways when there is only one coin left to consider.
There was a problem hiding this comment.
Agreed. Checking that before doing cache lookup avoids unnecessary tuple creation and dictionary access
Learners, PR Template
Self checklist
Changelist
I tried to improve the performance by :
Questions
No question