Skip to content

Created greedy_coin_change.py file #3805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 20, 2021
48 changes: 48 additions & 0 deletions greedy_method/greedy_coin_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Test cases:
find_minimum_change(987)
500 100 100 100 100 50 20 10 50 2
find_minimum_change(500)
500
find_minimum_change(0)
The total value cannot be zero or negetive
find_minimum_change(-96)
The total value cannot be zero or negetive
find_minimum_change(56)
50 5 1
"""


def find_minimum_change(V):
total_value = int(V)
# All denominations of Indian Currency
denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000]
length = len(denominations)

# Initialize Result
answer = []

# Traverse through all denomination
i = length - 1
while i >= 0:

# Find denominations
while int(total_value) >= int(denominations[i]):
total_value -= int(denominations[i])
answer.append(denominations[i]) # Append the "answers" array

i -= 1

# Print result
for i in range(len(answer)):
print(answer[i], end=" ")


# Driver Code
if __name__ == "__main__":
n = input("Enter the change you want to make in Indian Currency: ").strip()
if int(n) == 0 or int(n) < 0:
print("The total value cannot be zero or negetive.")
else:
print("Following is minimal number", "of change for", n, ": ", end="")
find_minimum_change(n)