Skip to content

Commit 4bdc334

Browse files
Add files via upload
Altered the code according to the changes that were requested.
1 parent 205c502 commit 4bdc334

File tree

1 file changed

+68
-29
lines changed

1 file changed

+68
-29
lines changed

greedy_method/greedy_coin_change.py

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,87 @@
11
"""
22
Test cases:
3-
find_minimum_change(987)
4-
500 100 100 100 100 50 20 10 50 2
5-
find_minimum_change(500)
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
636
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
37+
1000
38+
Enter the change you want to make in Indian Currency: 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
1341
"""
1442

1543

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)
44+
def find_minimum_change(denominations: list, value: int) -> list:
45+
46+
total_value = int(value)
2147

2248
# Initialize Result
2349
answer = []
2450

2551
# Traverse through all denomination
26-
i = length - 1
27-
while i >= 0:
52+
for denomination in reversed(denominations):
2853

2954
# 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
55+
while int(total_value) >= int(denomination):
56+
total_value -= int(denomination)
57+
answer.append(denomination) # Append the "answers" array
3558

36-
# Print result
37-
for i in range(len(answer)):
38-
print(answer[i], end=" ")
59+
return answer
3960

4061

4162
# Driver Code
4263
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.")
64+
65+
denominations = list()
66+
value = 0
67+
68+
if input("Do you want to enter your denominations ? (Y/N) :") == "Y":
69+
n = int(input("Enter number of denomination:"))
70+
71+
for i in range(0, n):
72+
denominations.append(int(input().strip()))
73+
value = input("Enter the change you want to make in Indian Currency: ").strip()
74+
else:
75+
# All denominations of Indian Currency if user does not enter
76+
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
77+
value = input("Enter the change you want to make: ").strip()
78+
79+
if int(value) == 0 or int(value) < 0:
80+
print("The total value cannot be zero or negative.")
81+
4682
else:
47-
print("Following is minimal number", "of change for", n, ": ", end="")
48-
find_minimum_change(n)
83+
print("Following is minimal ", "change for", value, ": ")
84+
answer = find_minimum_change(denominations, value)
85+
# Print result
86+
for i in range(len(answer)):
87+
print(answer[i], end=" ")

0 commit comments

Comments
 (0)