Skip to content

Commit 4e70c92

Browse files
DevanshiPatel18github-actionsdhruvmanila
authored
feat: Add greedy_coin_change.py algorithm (TheAlgorithms#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 <[email protected]>
1 parent 41da3dc commit 4e70c92

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

DIRECTORY.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@
267267
* Tests
268268
* [Test Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/tests/test_send_file.py)
269269

270+
## Fractals
271+
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/fractals/koch_snowflake.py)
272+
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/fractals/mandelbrot.py)
273+
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/fractals/sierpinski_triangle.py)
274+
270275
## Fuzzy Logic
271276
* [Fuzzy Operations](https://github.com/TheAlgorithms/Python/blob/master/fuzzy_logic/fuzzy_operations.py)
272277

@@ -279,8 +284,6 @@
279284

280285
## Graphics
281286
* [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py)
282-
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/graphics/koch_snowflake.py)
283-
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/graphics/mandelbrot.py)
284287
* [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py)
285288

286289
## Graphs
@@ -432,6 +435,7 @@
432435
* [Gamma](https://github.com/TheAlgorithms/Python/blob/master/maths/gamma.py)
433436
* [Gaussian](https://github.com/TheAlgorithms/Python/blob/master/maths/gaussian.py)
434437
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greatest_common_divisor.py)
438+
* [Greedy Coin Change](https://github.com/TheAlgorithms/Python/blob/master/maths/greedy_coin_change.py)
435439
* [Hardy Ramanujanalgo](https://github.com/TheAlgorithms/Python/blob/master/maths/hardy_ramanujanalgo.py)
436440
* [Is Square Free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py)
437441
* [Jaccard Similarity](https://github.com/TheAlgorithms/Python/blob/master/maths/jaccard_similarity.py)
@@ -547,7 +551,6 @@
547551
* [Primelib](https://github.com/TheAlgorithms/Python/blob/master/other/primelib.py)
548552
* [Scoring Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/scoring_algorithm.py)
549553
* [Sdes](https://github.com/TheAlgorithms/Python/blob/master/other/sdes.py)
550-
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py)
551554
* [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
552555
* [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py)
553556
* [Two Pointer](https://github.com/TheAlgorithms/Python/blob/master/other/two_pointer.py)

maths/greedy_coin_change.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Test cases:
3+
Do you want to enter your denominations ? (Y/N) :N
4+
Enter the change you want to make in Indian Currency: 987
5+
Following is minimal change for 987 :
6+
500 100 100 100 100 50 20 10 5 2
7+
8+
Do you want to enter your denominations ? (Y/N) :Y
9+
Enter number of denomination:10
10+
1
11+
5
12+
10
13+
20
14+
50
15+
100
16+
200
17+
500
18+
1000
19+
2000
20+
Enter the change you want to make: 18745
21+
Following is minimal change for 18745 :
22+
2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5
23+
24+
Do you want to enter your denominations ? (Y/N) :N
25+
Enter the change you want to make: 0
26+
The total value cannot be zero or negative.
27+
Do you want to enter your denominations ? (Y/N) :N
28+
Enter the change you want to make: -98
29+
The total value cannot be zero or negative.
30+
31+
Do you want to enter your denominations ? (Y/N) :Y
32+
Enter number of denomination:5
33+
1
34+
5
35+
100
36+
500
37+
1000
38+
Enter the change you want to make: 456
39+
Following is minimal change for 456 :
40+
100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1
41+
"""
42+
43+
44+
def find_minimum_change(denominations: list[int], value: int) -> 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)
48+
[2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5]
49+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987)
50+
[500, 100, 100, 100, 100, 50, 20, 10, 5, 2]
51+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 0)
52+
[]
53+
>>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], -98)
54+
[]
55+
>>> find_minimum_change([1, 5, 100, 500, 1000], 456)
56+
[100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1]
57+
"""
58+
total_value = int(value)
59+
60+
# Initialize Result
61+
answer = []
62+
63+
# Traverse through all denomination
64+
for denomination in reversed(denominations):
65+
66+
# Find denominations
67+
while int(total_value) >= int(denomination):
68+
total_value -= int(denomination)
69+
answer.append(denomination) # Append the "answers" array
70+
71+
return answer
72+
73+
74+
# Driver Code
75+
if __name__ == "__main__":
76+
77+
denominations = list()
78+
value = 0
79+
80+
if (
81+
input("Do you want to enter your denominations ? (yY/n): ").strip().lower()
82+
== "y"
83+
):
84+
n = int(input("Enter the number of denominations you want to add: ").strip())
85+
86+
for i in range(0, n):
87+
denominations.append(int(input(f"Denomination {i}: ").strip()))
88+
value = input("Enter the change you want to make in Indian Currency: ").strip()
89+
else:
90+
# All denominations of Indian Currency if user does not enter
91+
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
92+
value = input("Enter the change you want to make: ").strip()
93+
94+
if int(value) == 0 or int(value) < 0:
95+
print("The total value cannot be zero or negative.")
96+
97+
else:
98+
print(f"Following is minimal change for {value}: ")
99+
answer = find_minimum_change(denominations, value)
100+
# Print result
101+
for i in range(len(answer)):
102+
print(answer[i], end=" ")

0 commit comments

Comments
 (0)