Skip to content

Instantly share code, notes, and snippets.

@michaelminter
Created August 31, 2016 01:21
Show Gist options
  • Save michaelminter/e9d5cdb31d9fd6deca4df5d8c29b0a8d to your computer and use it in GitHub Desktop.
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.
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