-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcc30fe
commit 1135379
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/python3 | ||
"""Change comes from within""" | ||
|
||
|
||
def makeChange(coins, total): | ||
"""Given a pile of coins of different values, determine the fewest | ||
number of coins needed to meet a given amount total. | ||
Arguments: | ||
coins {list} -- List of the values of the coins in your possession | ||
total {int} -- Total to reach | ||
Returns: | ||
[int] -- Fewest number of coins needed to meet total | ||
0 if total is 0 or less | ||
-1 if total cannot be met by any number of coins you have | ||
""" | ||
if total <= 0: | ||
return 0 | ||
|
||
coins.sort(reverse=True) | ||
num_coins = 0 | ||
for coin in coins: | ||
if total <= 0: | ||
break | ||
num_coins += total // coin | ||
total = total % coin | ||
|
||
if total > 0: | ||
return -1 | ||
return num_coins |