1
+ import sys
1
2
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
-
46
3
def find_minimum_change (denominations : list [int ], value : int ) -> list [int ]:
47
4
"""
48
5
Find the minimum change from the given denominations and value.
49
-
50
6
Args:
51
7
denominations (list[int]): List of available denominations.
52
8
value (int): The amount of money to be changed.
53
-
54
9
Returns:
55
10
list[int]: List of denominations representing the minimal change.
56
-
57
11
Examples:
58
12
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745)
59
13
[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]:
66
20
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
67
21
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
68
22
"""
69
-
70
23
# Sort denominations in descending order (biggest first)
71
24
denominations .sort (reverse = True )
72
-
73
25
# Initialize Result
74
26
answer = []
75
-
76
27
# Find minimal change using largest denominations first
77
28
for denomination in denominations :
78
29
while value >= denomination :
79
30
value -= denomination
80
31
answer .append (denomination )
81
-
82
32
return answer
83
-
84
-
85
33
# Driver Code
86
34
if __name__ == "__main__" :
87
35
# Run doctest
88
36
testmod ()
89
-
90
37
denominations = []
91
38
value = 0
92
-
93
39
if (
94
40
input ("Do you want to enter your denominations ? (y/n): " ).strip ().lower ()
95
41
== "y"
@@ -105,19 +51,17 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]:
105
51
)
106
52
except ValueError :
107
53
print ("Invalid input. Please enter valid numbers." )
108
- exit (1 )
54
+ sys . exit (1 )
109
55
else :
110
56
# Default denominations for Indian Currency
111
57
denominations = [1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 2000 ]
112
58
try :
113
59
value = int (input ("Enter the change you want to make: " ).strip ())
114
60
except ValueError :
115
61
print ("Invalid input. Please enter a valid number." )
116
- exit (1 )
117
-
62
+ sys .exit (1 )
118
63
# Ensure denominations are sorted in descending order
119
64
denominations .sort (reverse = True )
120
-
121
65
if value <= 0 :
122
66
print ("The total value cannot be zero or negative." )
123
67
else :
0 commit comments