Skip to content

Commit 6a5c15a

Browse files
Added Greedy change file in Maths folder.
1 parent a5ef4f3 commit 6a5c15a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

maths/greedy_coin_change.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
Test cases:
3+
Do you want to enter your denominations ? (Y/N) :N
4+
Enter the change you want to make in Indian Currency: 987
5+
Following is minimal change for 987 :
6+
500 100 100 100 100 50 20 10 5 2
7+
8+
Do you want to enter your denominations ? (Y/N) :Y
9+
Enter number of denomination:10
10+
1
11+
5
12+
10
13+
20
14+
50
15+
100
16+
200
17+
500
18+
1000
19+
2000
20+
Enter the change you want to make: 18745
21+
Following is minimal change for 18745 :
22+
2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5
23+
24+
Do you want to enter your denominations ? (Y/N) :N
25+
Enter the change you want to make: 0
26+
The total value cannot be zero or negative.
27+
Do you want to enter your denominations ? (Y/N) :N
28+
Enter the change you want to make: -98
29+
The total value cannot be zero or negative.
30+
31+
Do you want to enter your denominations ? (Y/N) :Y
32+
Enter number of denomination:5
33+
1
34+
5
35+
100
36+
500
37+
1000
38+
Enter the change you want to make: 456
39+
Following is minimal change for 456 :
40+
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
41+
"""
42+
43+
44+
def find_minimum_change(denominations: list, value: int) -> list:
45+
"""
46+
Find the minimum change from the given denominations and value
47+
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000,2000], 18745)
48+
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
49+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
50+
[500, 100, 100, 100, 100, 50, 20, 10, 5, 2]
51+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 0)
52+
[]
53+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], -98)
54+
[]
55+
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
56+
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
57+
"""
58+
total_value = int(value)
59+
60+
# Initialize Result
61+
answer = []
62+
63+
# Traverse through all denomination
64+
for denomination in reversed(denominations):
65+
66+
# Find denominations
67+
while int(total_value) >= int(denomination):
68+
total_value -= int(denomination)
69+
answer.append(denomination) # Append the "answers" array
70+
71+
return answer
72+
73+
74+
# Driver Code
75+
if __name__ == "__main__":
76+
77+
denominations = list()
78+
value = 0
79+
80+
if input("Do you want to enter your denominations ? (Y/N) :") == "Y":
81+
n = int(input("Enter number of denomination:"))
82+
83+
for i in range(0, n):
84+
denominations.append(int(input().strip()))
85+
value = input("Enter the change you want to make in Indian Currency: ").strip()
86+
else:
87+
# All denominations of Indian Currency if user does not enter
88+
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
89+
value = input("Enter the change you want to make: ").strip()
90+
91+
if int(value) == 0 or int(value) < 0:
92+
print("The total value cannot be zero or negative.")
93+
94+
else:
95+
print("Following is minimal ", "change for", value, ": ")
96+
answer = find_minimum_change(denominations, value)
97+
# Print result
98+
for i in range(len(answer)):
99+
print(answer[i], end=" ")

0 commit comments

Comments
 (0)