Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b8a4e5b

Browse files
authoredFeb 16, 2025··
Update minimum_coin_change.py
1 parent 738253e commit b8a4e5b

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed
 

‎greedy_methods/minimum_coin_change.py

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import sys
2+
from doctest import testmod
3+
14
"""
25
Test cases:
36
Do you want to enter your denominations ? (Y/N) :N
@@ -41,10 +44,16 @@
4144
"""
4245

4346

44-
def find_minimum_change(denominations: list[int], value: str) -> list[int]:
47+
def find_minimum_change(denominations: list[int], value: int) -> list[int]:
4548
"""
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)
4857
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
4958
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
5059
[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]:
5564
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
5665
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
5766
"""
58-
total_value = int(value)
59-
67+
# Sort denominations in descending order (biggest first)
68+
denominations.sort(reverse=True)
6069
# Initialize Result
6170
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)
7076
return answer
7177

7278

7379
# Driver Code
7480
if __name__ == "__main__":
81+
# Run doctest
82+
testmod()
7583
denominations = []
76-
value = "0"
77-
84+
value = 0
7885
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()
8087
== "y"
8188
):
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)
87101
else:
88-
# All denominations of Indian Currency if user does not enter
102+
# Default denominations for Indian Currency
89103
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:
93112
print("The total value cannot be zero or negative.")
94-
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)
Please sign in to comment.