Skip to content

Commit ec2723f

Browse files
authored
Update minimum_coin_change.py
Change with format
1 parent 7286800 commit ec2723f

File tree

1 file changed

+3
-59
lines changed

1 file changed

+3
-59
lines changed

greedy_methods/minimum_coin_change.py

+3-59
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
1+
import sys
12
from doctest import testmod
2-
3-
"""
4-
Test cases:
5-
Do you want to enter your denominations ? (Y/N) :N
6-
Enter the change you want to make in Indian Currency: 987
7-
Following is minimal change for 987 :
8-
500 100 100 100 100 50 20 10 5 2
9-
10-
Do you want to enter your denominations ? (Y/N) :Y
11-
Enter number of denomination:10
12-
1
13-
5
14-
10
15-
20
16-
50
17-
100
18-
200
19-
500
20-
1000
21-
2000
22-
Enter the change you want to make: 18745
23-
Following is minimal change for 18745 :
24-
2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5
25-
26-
Do you want to enter your denominations ? (Y/N) :N
27-
Enter the change you want to make: 0
28-
The total value cannot be zero or negative.
29-
Do you want to enter your denominations ? (Y/N) :N
30-
Enter the change you want to make: -98
31-
The total value cannot be zero or negative.
32-
33-
Do you want to enter your denominations ? (Y/N) :Y
34-
Enter number of denomination:5
35-
1
36-
5
37-
100
38-
500
39-
1000
40-
Enter the change you want to make: 456
41-
Following is minimal change for 456 :
42-
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
43-
"""
44-
45-
463
def find_minimum_change(denominations: list[int], value: int) -> list[int]:
474
"""
485
Find the minimum change from the given denominations and value.
49-
506
Args:
517
denominations (list[int]): List of available denominations.
528
value (int): The amount of money to be changed.
53-
549
Returns:
5510
list[int]: List of denominations representing the minimal change.
56-
5711
Examples:
5812
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745)
5913
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
@@ -66,30 +20,22 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]:
6620
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
6721
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
6822
"""
69-
7023
# Sort denominations in descending order (biggest first)
7124
denominations.sort(reverse=True)
72-
7325
# Initialize Result
7426
answer = []
75-
7627
# Find minimal change using largest denominations first
7728
for denomination in denominations:
7829
while value >= denomination:
7930
value -= denomination
8031
answer.append(denomination)
81-
8232
return answer
83-
84-
8533
# Driver Code
8634
if __name__ == "__main__":
8735
# Run doctest
8836
testmod()
89-
9037
denominations = []
9138
value = 0
92-
9339
if (
9440
input("Do you want to enter your denominations ? (y/n): ").strip().lower()
9541
== "y"
@@ -105,19 +51,17 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]:
10551
)
10652
except ValueError:
10753
print("Invalid input. Please enter valid numbers.")
108-
exit(1)
54+
sys.exit(1)
10955
else:
11056
# Default denominations for Indian Currency
11157
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
11258
try:
11359
value = int(input("Enter the change you want to make: ").strip())
11460
except ValueError:
11561
print("Invalid input. Please enter a valid number.")
116-
exit(1)
117-
62+
sys.exit(1)
11863
# Ensure denominations are sorted in descending order
11964
denominations.sort(reverse=True)
120-
12165
if value <= 0:
12266
print("The total value cannot be zero or negative.")
12367
else:

0 commit comments

Comments
 (0)