1
+ import sys
2
+ from doctest import testmod
3
+
1
4
"""
2
5
Test cases:
3
6
Do you want to enter your denominations ? (Y/N) :N
41
44
"""
42
45
43
46
44
- def find_minimum_change (denominations : list [int ], value : str ) -> list [int ]:
47
+ def find_minimum_change (denominations : list [int ], value : int ) -> list [int ]:
45
48
"""
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)
49
+ Find the minimum change from the given denominations and value.
50
+ Args:
51
+ denominations (list[int]): List of available denominations.
52
+ value (int): The amount of money to be changed.
53
+ Returns:
54
+ list[int]: List of denominations representing the minimal change.
55
+ Examples:
56
+ >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745)
48
57
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
49
58
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
50
59
[500, 100, 100, 100, 100, 50, 20, 10, 5, 2]
@@ -55,46 +64,53 @@ def find_minimum_change(denominations: list[int], value: str) -> list[int]:
55
64
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
56
65
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
57
66
"""
58
- total_value = int ( value )
59
-
67
+ # Sort denominations in descending order (biggest first )
68
+ denominations . sort ( reverse = True )
60
69
# Initialize Result
61
70
answer = []
62
-
63
- # Traverse through all denomination
64
- for denomination in reversed (denominations ):
65
- # Find denominations
66
- while int (total_value ) >= int (denomination ):
67
- total_value -= int (denomination )
68
- answer .append (denomination ) # Append the "answers" array
69
-
71
+ # Find minimal change using largest denominations first
72
+ for denomination in denominations :
73
+ while value >= denomination :
74
+ value -= denomination
75
+ answer .append (denomination )
70
76
return answer
71
77
72
78
73
79
# Driver Code
74
80
if __name__ == "__main__" :
81
+ # Run doctest
82
+ testmod ()
75
83
denominations = []
76
- value = "0"
77
-
84
+ value = 0
78
85
if (
79
- input ("Do you want to enter your denominations ? (yY /n): " ).strip ().lower ()
86
+ input ("Do you want to enter your denominations ? (y /n): " ).strip ().lower ()
80
87
== "y"
81
88
):
82
- n = int (input ("Enter the number of denominations you want to add: " ).strip ())
83
-
84
- for i in range (n ):
85
- denominations .append (int (input (f"Denomination { i } : " ).strip ()))
86
- value = input ("Enter the change you want to make in Indian Currency: " ).strip ()
89
+ try :
90
+ n = int (
91
+ input ("Enter the number of denominations you want to add: " ).strip ()
92
+ )
93
+ for i in range (n ):
94
+ denominations .append (int (input (f"Denomination { i + 1 } : " ).strip ()))
95
+ value = int (
96
+ input ("Enter the change you want to make in Indian Currency: " ).strip ()
97
+ )
98
+ except ValueError :
99
+ print ("Invalid input. Please enter valid numbers." )
100
+ sys .exit (1 )
87
101
else :
88
- # All denominations of Indian Currency if user does not enter
102
+ # Default denominations for Indian Currency
89
103
denominations = [1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 2000 ]
90
- value = input ("Enter the change you want to make: " ).strip ()
91
-
92
- if int (value ) == 0 or int (value ) < 0 :
104
+ try :
105
+ value = int (input ("Enter the change you want to make: " ).strip ())
106
+ except ValueError :
107
+ print ("Invalid input. Please enter a valid number." )
108
+ sys .exit (1 )
109
+ # Ensure denominations are sorted in descending order
110
+ denominations .sort (reverse = True )
111
+ if value <= 0 :
93
112
print ("The total value cannot be zero or negative." )
94
-
95
113
else :
96
114
print (f"Following is minimal change for { value } : " )
97
115
answer = find_minimum_change (denominations , value )
98
- # Print result
99
- for i in range (len (answer )):
100
- print (answer [i ], end = " " )
116
+ print (" " .join (map (str , answer ))) # Optimized printing format
0 commit comments