Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sprint-2/improve_with_caches/fibonacci/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
class Fibonacci:
def __init__(self):
self.cache = {0: 0, 1: 1}

def __call__(self, n):
if n in self.cache:
return self.cache[n]

self.cache[n] = self(n - 1) + self(n - 2)
return self.cache[n]

fibonacci = Fibonacci()
2 changes: 2 additions & 0 deletions Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_3(self):
def test_10(self):
self.assertEqual(fibonacci(10), 55)

def test_50(self):
self.assertEqual(fibonacci(50), 12586269025)
def test_200(self):
self.assertEqual(fibonacci(200), 280571172992510140037611932413038677189525)

Expand Down
5 changes: 5 additions & 0 deletions Sprint-2/improve_with_caches/making_change/coin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Coin:
def __init__(self, name: str, value: int):
self.name = name
self.value = value

59 changes: 34 additions & 25 deletions Sprint-2/improve_with_caches/making_change/making_change.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
from typing import List
from functools import lru_cache
from coin import Coin

COINS = [
Coin("200 coins", 200),
Coin("100 coins", 100),
Coin("50 coins", 50),
Coin("20 coins", 20),
Coin("10 coins", 10),
Coin("5 coins", 5),
Coin("2 coins", 2),
Coin("1 coin", 1),
]


def ways_to_make_change(total: int) -> int:
"""
Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200, returns a count of all of the ways to make the passed total value.

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.
Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200,
returns the number of ways to make the total.
"""
return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1])
@lru_cache(maxsize=None)
def helper(remaining: int, index: int) -> int:

if remaining == 0:
return 1

if index == len(COINS):
return 0

ways = 0
coin = COINS[index]

def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
"""
Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
"""
if total == 0 or len(coins) == 0:
return 0

ways = 0
for coin_index in range(len(coins)):
coin = coins[coin_index]
count_of_coin = 1
while coin * count_of_coin <= total:
total_from_coins = coin * count_of_coin
if total_from_coins == total:
ways += 1
else:
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
ways += intermediate
count_of_coin += 1
return ways
max_count = remaining

for count in range(max_count + 1):
new_remaining = remaining - count * coin.value
ways += helper(new_remaining, index + 1)

return ways

return helper(total, 0)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_7(self):
def test_17(self):
self.assertEqual(ways_to_make_change(17), 28)

def test_100(self):
self.assertEqual(ways_to_make_change(100), 4563)
def test_9176(self):
self.assertEqual(ways_to_make_change(9176), 628431158425225)

Expand Down