Skip to content

Commit ff961fa

Browse files
committed
Update minimum_coin_change.py
Fix: Improve minimum coin change logic & add doctest - Removed unnecessary int() conversions to improve efficiency. - Updated function parameter type (value: str → value: int) to avoid redundant type casting. - Added try-except handling to prevent ValueError when user input is not a number. - Implemented testmod() to enable automated doctest verification. - Fixed issue where input denominations were not sorted in descending order, which previously led to incorrect results.
1 parent 738253e commit ff961fa

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

greedy_methods/minimum_coin_change.py

+45-29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from doctest import testmod
12
"""
23
Test cases:
34
Do you want to enter your denominations ? (Y/N) :N
@@ -39,12 +40,19 @@
3940
Following is minimal change for 456 :
4041
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
4142
"""
43+
def find_minimum_change(denominations: list[int], value: int) -> list[int]:
44+
"""
45+
Find the minimum change from the given denominations and value.
4246
47+
Args:
48+
denominations (list[int]): List of available denominations.
49+
value (int): The amount of money to be changed.
4350
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)
4856
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
4957
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
5058
[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]:
5563
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
5664
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
5765
"""
58-
total_value = int(value)
66+
67+
# Sort denominations in descending order (biggest first)
68+
denominations.sort(reverse=True)
5969

6070
# Initialize Result
6171
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)
6978

7079
return answer
7180

7281

7382
# Driver Code
7483
if __name__ == "__main__":
75-
denominations = []
76-
value = "0"
84+
# Run doctest
85+
testmod()
7786

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
8389

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)
8799
else:
88-
# All denominations of Indian Currency if user does not enter
100+
# Default denominations for Indian Currency
89101
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)
91107

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)
94110

111+
if value <= 0:
112+
print("The total value cannot be zero or negative.")
95113
else:
96114
print(f"Following is minimal change for {value}: ")
97115
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

Comments
 (0)