Created
August 31, 2016 01:21
-
-
Save michaelminter/e9d5cdb31d9fd6deca4df5d8c29b0a8d to your computer and use it in GitHub Desktop.
Interviewzen test: Assuming an infinite supply of coins (1,5,10,25 cent denominations), programmatically make change for $0.98 using the least amount of coins possible.
This file contains hidden or 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
class MoneyHandling | |
def least_number_coins(amount_cents) | |
coins = [25, 10, 5, 1] | |
remaining = amount_cents | |
count = {} | |
coins.each do |coin| | |
if amount_cents > coin | |
remainder = (remaining / coin).floor | |
count[coin] = remainder | |
remaining -= coin * remainder | |
end | |
end | |
count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment