mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 09:38:27 +00:00
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.
This commit is contained in:
parent
738253e800
commit
ff961fa04a
@ -1,3 +1,4 @@
|
|||||||
|
from doctest import testmod
|
||||||
"""
|
"""
|
||||||
Test cases:
|
Test cases:
|
||||||
Do you want to enter your denominations ? (Y/N) :N
|
Do you want to enter your denominations ? (Y/N) :N
|
||||||
@ -39,12 +40,19 @@ Enter the change you want to make: 456
|
|||||||
Following is minimal change for 456 :
|
Following is minimal change for 456 :
|
||||||
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
|
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
|
||||||
"""
|
"""
|
||||||
|
def find_minimum_change(denominations: list[int], value: int) -> list[int]:
|
||||||
|
|
||||||
def find_minimum_change(denominations: list[int], value: str) -> list[int]:
|
|
||||||
"""
|
"""
|
||||||
Find the minimum change from the given denominations and value
|
Find the minimum change from the given denominations and value.
|
||||||
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000,2000], 18745)
|
|
||||||
|
Args:
|
||||||
|
denominations (list[int]): List of available denominations.
|
||||||
|
value (int): The amount of money to be changed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list[int]: List of denominations representing the minimal change.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745)
|
||||||
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
|
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
|
||||||
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
|
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
|
||||||
[500, 100, 100, 100, 100, 50, 20, 10, 5, 2]
|
[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]:
|
|||||||
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
|
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
|
||||||
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
|
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
|
||||||
"""
|
"""
|
||||||
total_value = int(value)
|
|
||||||
|
# Sort denominations in descending order (biggest first)
|
||||||
|
denominations.sort(reverse=True)
|
||||||
|
|
||||||
# Initialize Result
|
# Initialize Result
|
||||||
answer = []
|
answer = []
|
||||||
|
|
||||||
# Traverse through all denomination
|
# Find minimal change using largest denominations first
|
||||||
for denomination in reversed(denominations):
|
for denomination in denominations:
|
||||||
# Find denominations
|
while value >= denomination:
|
||||||
while int(total_value) >= int(denomination):
|
value -= denomination
|
||||||
total_value -= int(denomination)
|
answer.append(denomination)
|
||||||
answer.append(denomination) # Append the "answers" array
|
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
|
||||||
# Driver Code
|
# Driver Code
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# Run doctest
|
||||||
|
testmod()
|
||||||
|
|
||||||
denominations = []
|
denominations = []
|
||||||
value = "0"
|
value = 0
|
||||||
|
|
||||||
if (
|
if input("Do you want to enter your denominations ? (y/n): ").strip().lower() == "y":
|
||||||
input("Do you want to enter your denominations ? (yY/n): ").strip().lower()
|
try:
|
||||||
== "y"
|
n = int(input("Enter the number of denominations you want to add: ").strip())
|
||||||
):
|
for i in range(n):
|
||||||
n = int(input("Enter the number of denominations you want to add: ").strip())
|
denominations.append(int(input(f"Denomination {i+1}: ").strip()))
|
||||||
|
value = int(input("Enter the change you want to make in Indian Currency: ").strip())
|
||||||
for i in range(n):
|
except ValueError:
|
||||||
denominations.append(int(input(f"Denomination {i}: ").strip()))
|
print("Invalid input. Please enter valid numbers.")
|
||||||
value = input("Enter the change you want to make in Indian Currency: ").strip()
|
exit(1)
|
||||||
else:
|
else:
|
||||||
# All denominations of Indian Currency if user does not enter
|
# Default denominations for Indian Currency
|
||||||
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
|
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
|
||||||
value = input("Enter the change you want to make: ").strip()
|
try:
|
||||||
|
value = int(input("Enter the change you want to make: ").strip())
|
||||||
|
except ValueError:
|
||||||
|
print("Invalid input. Please enter a valid number.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
if int(value) == 0 or int(value) < 0:
|
# Ensure denominations are sorted in descending order
|
||||||
|
denominations.sort(reverse=True)
|
||||||
|
|
||||||
|
if value <= 0:
|
||||||
print("The total value cannot be zero or negative.")
|
print("The total value cannot be zero or negative.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"Following is minimal change for {value}: ")
|
print(f"Following is minimal change for {value}: ")
|
||||||
answer = find_minimum_change(denominations, value)
|
answer = find_minimum_change(denominations, value)
|
||||||
# Print result
|
print(" ".join(map(str, answer))) # Optimized printing format
|
||||||
for i in range(len(answer)):
|
|
||||||
print(answer[i], end=" ")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user