mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
feat: Add greedy_coin_change.py algorithm (#3805)
* Hacktoberfest: Add greedy_coin_change.py file added the file in greedy_methods folder to implement the same method Altered the code according to the changes that were requested. * Added doctests. doctests added to the function find_minimum_change. * Added Greedy change file in Maths folder. * updating DIRECTORY.md * Deleted Greedy Method Folder * updating DIRECTORY.md * Update greedy_coin_change.py * fix: black formatting issues Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
2dc2c99f2b
commit
dd757dce38
|
@ -267,6 +267,11 @@
|
|||
* Tests
|
||||
* [Test Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/tests/test_send_file.py)
|
||||
|
||||
## Fractals
|
||||
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/fractals/koch_snowflake.py)
|
||||
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/fractals/mandelbrot.py)
|
||||
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/fractals/sierpinski_triangle.py)
|
||||
|
||||
## Fuzzy Logic
|
||||
* [Fuzzy Operations](https://github.com/TheAlgorithms/Python/blob/master/fuzzy_logic/fuzzy_operations.py)
|
||||
|
||||
|
@ -279,8 +284,6 @@
|
|||
|
||||
## Graphics
|
||||
* [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py)
|
||||
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/graphics/koch_snowflake.py)
|
||||
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/graphics/mandelbrot.py)
|
||||
* [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py)
|
||||
|
||||
## Graphs
|
||||
|
@ -432,6 +435,7 @@
|
|||
* [Gamma](https://github.com/TheAlgorithms/Python/blob/master/maths/gamma.py)
|
||||
* [Gaussian](https://github.com/TheAlgorithms/Python/blob/master/maths/gaussian.py)
|
||||
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greatest_common_divisor.py)
|
||||
* [Greedy Coin Change](https://github.com/TheAlgorithms/Python/blob/master/maths/greedy_coin_change.py)
|
||||
* [Hardy Ramanujanalgo](https://github.com/TheAlgorithms/Python/blob/master/maths/hardy_ramanujanalgo.py)
|
||||
* [Is Square Free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py)
|
||||
* [Jaccard Similarity](https://github.com/TheAlgorithms/Python/blob/master/maths/jaccard_similarity.py)
|
||||
|
@ -547,7 +551,6 @@
|
|||
* [Primelib](https://github.com/TheAlgorithms/Python/blob/master/other/primelib.py)
|
||||
* [Scoring Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/scoring_algorithm.py)
|
||||
* [Sdes](https://github.com/TheAlgorithms/Python/blob/master/other/sdes.py)
|
||||
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py)
|
||||
* [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
|
||||
* [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py)
|
||||
* [Two Pointer](https://github.com/TheAlgorithms/Python/blob/master/other/two_pointer.py)
|
||||
|
|
102
maths/greedy_coin_change.py
Normal file
102
maths/greedy_coin_change.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
"""
|
||||
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]:
|
||||
"""
|
||||
Find the minimum change from the given denominations and value
|
||||
>>> 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]
|
||||
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
|
||||
[500, 100, 100, 100, 100, 50, 20, 10, 5, 2]
|
||||
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 0)
|
||||
[]
|
||||
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], -98)
|
||||
[]
|
||||
>>> 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]
|
||||
"""
|
||||
total_value = int(value)
|
||||
|
||||
# Initialize Result
|
||||
answer = []
|
||||
|
||||
# Traverse through all denomination
|
||||
for denomination in reversed(denominations):
|
||||
|
||||
# Find denominations
|
||||
while int(total_value) >= int(denomination):
|
||||
total_value -= int(denomination)
|
||||
answer.append(denomination) # Append the "answers" array
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
# Driver Code
|
||||
if __name__ == "__main__":
|
||||
|
||||
denominations = list()
|
||||
value = 0
|
||||
|
||||
if (
|
||||
input("Do you want to enter your denominations ? (yY/n): ").strip().lower()
|
||||
== "y"
|
||||
):
|
||||
n = int(input("Enter the number of denominations you want to add: ").strip())
|
||||
|
||||
for i in range(0, n):
|
||||
denominations.append(int(input(f"Denomination {i}: ").strip()))
|
||||
value = input("Enter the change you want to make in Indian Currency: ").strip()
|
||||
else:
|
||||
# All denominations of Indian Currency if user does not enter
|
||||
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
|
||||
value = input("Enter the change you want to make: ").strip()
|
||||
|
||||
if int(value) == 0 or int(value) < 0:
|
||||
print("The total value cannot be zero or negative.")
|
||||
|
||||
else:
|
||||
print(f"Following is minimal change for {value}: ")
|
||||
answer = find_minimum_change(denominations, value)
|
||||
# Print result
|
||||
for i in range(len(answer)):
|
||||
print(answer[i], end=" ")
|
Loading…
Reference in New Issue
Block a user