mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 09:38:27 +00:00
Update minimum_coin_change.py
Finished and formatted
This commit is contained in:
parent
72868008ad
commit
d34b0866d5
@ -1,59 +1,13 @@
|
|||||||
|
import sys
|
||||||
from doctest import testmod
|
from doctest import testmod
|
||||||
|
|
||||||
"""
|
|
||||||
Test cases:
|
|
||||||
Do you want to enter your denominations ? (Y/N) :N
|
|
||||||
Enter the change you want to make in Indian Currency: 987
|
|
||||||
Following is minimal change for 987 :
|
|
||||||
500 100 100 100 100 50 20 10 5 2
|
|
||||||
|
|
||||||
Do you want to enter your denominations ? (Y/N) :Y
|
|
||||||
Enter number of denomination:10
|
|
||||||
1
|
|
||||||
5
|
|
||||||
10
|
|
||||||
20
|
|
||||||
50
|
|
||||||
100
|
|
||||||
200
|
|
||||||
500
|
|
||||||
1000
|
|
||||||
2000
|
|
||||||
Enter the change you want to make: 18745
|
|
||||||
Following is minimal change for 18745 :
|
|
||||||
2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5
|
|
||||||
|
|
||||||
Do you want to enter your denominations ? (Y/N) :N
|
|
||||||
Enter the change you want to make: 0
|
|
||||||
The total value cannot be zero or negative.
|
|
||||||
Do you want to enter your denominations ? (Y/N) :N
|
|
||||||
Enter the change you want to make: -98
|
|
||||||
The total value cannot be zero or negative.
|
|
||||||
|
|
||||||
Do you want to enter your denominations ? (Y/N) :Y
|
|
||||||
Enter number of denomination:5
|
|
||||||
1
|
|
||||||
5
|
|
||||||
100
|
|
||||||
500
|
|
||||||
1000
|
|
||||||
Enter the change you want to make: 456
|
|
||||||
Following is minimal change for 456 :
|
|
||||||
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: int) -> list[int]:
|
||||||
"""
|
"""
|
||||||
Find the minimum change from the given denominations and value.
|
Find the minimum change from the given denominations and value.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
denominations (list[int]): List of available denominations.
|
denominations (list[int]): List of available denominations.
|
||||||
value (int): The amount of money to be changed.
|
value (int): The amount of money to be changed.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[int]: List of denominations representing the minimal change.
|
list[int]: List of denominations representing the minimal change.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745)
|
>>> 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]
|
||||||
@ -66,30 +20,22 @@ def find_minimum_change(denominations: list[int], value: int) -> 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]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Sort denominations in descending order (biggest first)
|
# Sort denominations in descending order (biggest first)
|
||||||
denominations.sort(reverse=True)
|
denominations.sort(reverse=True)
|
||||||
|
|
||||||
# Initialize Result
|
# Initialize Result
|
||||||
answer = []
|
answer = []
|
||||||
|
|
||||||
# Find minimal change using largest denominations first
|
# Find minimal change using largest denominations first
|
||||||
for denomination in denominations:
|
for denomination in denominations:
|
||||||
while value >= denomination:
|
while value >= denomination:
|
||||||
value -= denomination
|
value -= denomination
|
||||||
answer.append(denomination)
|
answer.append(denomination)
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
|
||||||
# Driver Code
|
# Driver Code
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Run doctest
|
# Run doctest
|
||||||
testmod()
|
testmod()
|
||||||
|
|
||||||
denominations = []
|
denominations = []
|
||||||
value = 0
|
value = 0
|
||||||
|
|
||||||
if (
|
if (
|
||||||
input("Do you want to enter your denominations ? (y/n): ").strip().lower()
|
input("Do you want to enter your denominations ? (y/n): ").strip().lower()
|
||||||
== "y"
|
== "y"
|
||||||
@ -105,7 +51,7 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Invalid input. Please enter valid numbers.")
|
print("Invalid input. Please enter valid numbers.")
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
# Default denominations for Indian Currency
|
# 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]
|
||||||
@ -113,11 +59,9 @@ if __name__ == "__main__":
|
|||||||
value = int(input("Enter the change you want to make: ").strip())
|
value = int(input("Enter the change you want to make: ").strip())
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Invalid input. Please enter a valid number.")
|
print("Invalid input. Please enter a valid number.")
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Ensure denominations are sorted in descending order
|
# Ensure denominations are sorted in descending order
|
||||||
denominations.sort(reverse=True)
|
denominations.sort(reverse=True)
|
||||||
|
|
||||||
if value <= 0:
|
if value <= 0:
|
||||||
print("The total value cannot be zero or negative.")
|
print("The total value cannot be zero or negative.")
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user