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