Skip to content

Commit b5928e5

Browse files
author
Hamid Gasmi
committed
#168 refactored + test cases added
1 parent ab98b4f commit b5928e5

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#python3
21
import sys
32

43
# 1. Express a solution mathematically: Change(M) = min{ Change(M - Coin[i]) } + 1 for all i between 0 and |Coins| - 1
@@ -12,29 +11,35 @@
1211
# 1. Bottom up solution
1312
# 2. Recursive + Memorization solution
1413

15-
# Running Time: O(monney * |coins|)
16-
# 1. Coins array could be sorted by Counting Sort algorithm
17-
# 2. 2 for loops: monney * |coins|
18-
# Space Complexity: O(monney)
19-
def find_change_buttom_up(monney, coins):
20-
21-
changes = [0]
22-
coins.sort()
14+
class CoinChanger:
15+
def __init__(self, coins):
16+
self.coins = coins.copy()
17+
self.coins.sort()
18+
19+
# Solution: buttom up solution
20+
# Running Time: O(monney * |coins|): 2 for loops: monney * |coins|
21+
# Space Complexity: O(monney)
22+
def make_change(self, money):
2323

24-
for m in range(1, monney + 1, 1):
25-
changes.append(sys.maxsize)
26-
for coin in coins:
27-
if m >= coin:
24+
changes = [0]
25+
26+
for m in range(1, money + 1, 1):
27+
changes.append(sys.maxsize)
28+
for coin in self.coins:
29+
if coin > m:
30+
continue
31+
2832
candidate_change = changes[ m - coin ] + 1
2933
changes[m] = min(changes[m], candidate_change)
30-
else:
31-
break
32-
33-
return changes[monney]
34+
35+
return changes[money]
3436

3537
if __name__ == "__main__":
3638
money = int(sys.stdin.readline().strip())
37-
coins = list(map(int, sys.stdin.readline().strip().split(',')))
3839

39-
print(find_change_buttom_up(money, coins))
40+
coins = list(map(int, sys.stdin.readline().strip().split(',')))
41+
42+
coinchanger = CoinChanger(coins)
43+
44+
print(coinchanger.make_change(money))
4045

6-dynamic-programming-applications-in-machine-learning-and-genomics/1-sequence-alignment-1/change_coins_memorization.py

+33-25
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,46 @@
1111
# 3. Solutions: we can solve this problem in 2 ways:
1212
# 1. Bottom up solution
1313
# 2. Recursive + Memorization solution
14+
class CoinChanger:
15+
def __init__(self, coins):
16+
self.coins = coins.copy()
17+
self.coins.sort()
1418

15-
def find_change_memorized(m, coins, changes):
16-
if changes[m] != sys.maxsize:
17-
return changes[m]
18-
19-
for coin in coins:
20-
if changes[m] == -1 or m < coin:
21-
continue
22-
23-
candidate_change = find_change_memorized(m - coin, coins, changes)
24-
if candidate_change == -1:
25-
continue
19+
# Solution: top down
20+
# Running Time: O(monney * |coins|): 2 for loops: monney * |coins|
21+
# Space Complexity: O(monney)
22+
def make_change(self, monney):
2623

27-
changes[m] = min(changes[m], candidate_change + 1)
24+
changes = [sys.maxsize for i in range(money + 1)]
25+
changes[0] = 0
26+
for coin in coins:
27+
if money >= coin:
28+
changes[coin] = 1
29+
30+
return self.make_change_memorized(monney, coins, changes)
2831

29-
if changes[m] == sys.maxsize:
30-
changes[m] = -1
31-
32-
return changes[m]
32+
def make_change_memorized(self, m, coins, changes):
33+
if changes[m] != sys.maxsize:
34+
return changes[m]
35+
36+
for coin in coins:
37+
if changes[m] == -1 or m < coin:
38+
continue
39+
40+
candidate_change = self.make_change_memorized(m - coin, coins, changes)
41+
if candidate_change == -1:
42+
continue
3343

34-
def find_change(monney, coins):
35-
36-
changes = [sys.maxsize for i in range(money + 1)]
37-
changes[0] = 0
38-
for coin in coins:
39-
if money >= coin:
40-
changes[coin] = 1
44+
changes[m] = min(changes[m], candidate_change + 1)
4145

42-
return(find_change_memorized(monney, coins, changes))
46+
if changes[m] == sys.maxsize:
47+
changes[m] = -1
48+
49+
return changes[m]
4350

4451
if __name__ == "__main__":
4552
money = int(sys.stdin.readline().strip())
4653
coins = list(map(int, sys.stdin.readline().strip().split(',')))
4754

48-
print(find_change(money, coins))
55+
coinchanger = CoinChanger(coins)
56+
print(coinchanger.make_change(money))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3
2+
2

0 commit comments

Comments
 (0)