Skip to content

Commit 205c502

Browse files
Hacktoberfest: Created greedy_coin_change.py file
added the file in greedy_methods folder to implement the same method
1 parent aebf9bd commit 205c502

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

greedy_method/greedy_coin_change.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Test cases:
3+
find_minimum_change(987)
4+
500 100 100 100 100 50 20 10 50 2
5+
find_minimum_change(500)
6+
500
7+
find_minimum_change(0)
8+
The total value cannot be zero or negetive
9+
find_minimum_change(-96)
10+
The total value cannot be zero or negetive
11+
find_minimum_change(56)
12+
50 5 1
13+
"""
14+
15+
16+
def find_minimum_change(V):
17+
total_value = int(V)
18+
# All denominations of Indian Currency
19+
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
20+
length = len(denominations)
21+
22+
# Initialize Result
23+
answer = []
24+
25+
# Traverse through all denomination
26+
i = length - 1
27+
while i >= 0:
28+
29+
# Find denominations
30+
while int(total_value) >= int(denominations[i]):
31+
total_value -= int(denominations[i])
32+
answer.append(denominations[i]) # Append the "answers" array
33+
34+
i -= 1
35+
36+
# Print result
37+
for i in range(len(answer)):
38+
print(answer[i], end=" ")
39+
40+
41+
# Driver Code
42+
if __name__ == "__main__":
43+
n = input("Enter the change you want to make in Indian Currency: ").strip()
44+
if int(n) == 0 or int(n) < 0:
45+
print("The total value cannot be zero or negetive.")
46+
else:
47+
print("Following is minimal number", "of change for", n, ": ", end="")
48+
find_minimum_change(n)

0 commit comments

Comments
 (0)