MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_minimum_change

Function find_minimum_change

greedy_methods/minimum_coin_change.py:44–70  ·  view source on GitHub ↗

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)

(denominations: list[int], value: str)

Source from the content-addressed store, hash-verified

42
43
44def find_minimum_change(denominations: list[int], value: str) -> 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 # Find denominations
66 while int(total_value) >= int(denomination):
67 total_value -= int(denomination)
68 answer.append(denomination) # Append the "answers" array
69
70 return answer
71
72
73# Driver Code

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected