Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| cache = { | ||
| 0: 0, | ||
| 1: 1 | ||
| } |
There was a problem hiding this comment.
This variable is not used anywhere in this file.
There was a problem hiding this comment.
Thank, i deleted it now while cache variable is never used in fibronacci function
There was a problem hiding this comment.
Wouldn't deleting it beat the purpose of this exercise?
There was a problem hiding this comment.
Thank you for pointing out. I've kept the original Fibonacci implementation and updated it to use the existing cache dictionary. How do you think?
| @@ -29,4 +35,7 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int: | |||
| intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:]) | |||
| ways += intermediate | |||
| count_of_coin += 1 | |||
There was a problem hiding this comment.
Array and tuple creations are relatively costly operations.
From lines 27 and 35, 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 35 (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.
There was a problem hiding this comment.
Thanks for the feedback! I updated the implementation to use a coin index instead of creating sliced lists. The memoization cache now uses (total, coin_index) as its key, avoiding repeated tuple and list creation while preserving the logic for counting combinations.
There was a problem hiding this comment.
Sounds good.
I don't see any new commits on GitHub yet.
There was a problem hiding this comment.
Do apologize, i forgot to commit and push. i just did it
| @@ -8,34 +8,32 @@ def ways_to_make_change(total: int) -> int: | |||
|
|
|||
| For instance, there are two ways to make a value of 3: with 3x 1 coins, or with 1x 1 coin and 1x 2 coin. | |||
| """ | |||
| return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) | |||
There was a problem hiding this comment.
Thank you for asking.I changed the coin denominations to a tuple because they are fixed and do not need to be modified. The main optimization was replacing list slicing with a coin index, avoiding the creation of new lists during recursive calls. The cache now uses (total, coin_index) as its key.
Self checklist
Task code
CYF-1182
Changelist
. Updated the Fibonacci function to use the existing cache dictionary, avoiding repeated calculations and
improving performance through memoisation.