Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created January 27, 2012 16:43
Show Gist options
  • Save dmerrick/1689685 to your computer and use it in GitHub Desktop.
Save dmerrick/1689685 to your computer and use it in GitHub Desktop.
class ChangeCalculator
QUARTER = 0.25
DIME = 0.10
NICKEL = 0.05
PENNY = 0.01
COINS = [QUARTER, DIME, NICKEL, PENNY]
def calculate(x)
remaining = x
$h = COINS.inject({}) do |hash, coin|
hash[coin] = (remaining / coin).floor
remaining = remaining % coin
hash
end
end
def output(hash)
hash.each {|value, num| printf("number of %1.2f coins = %d\n", value, num)}
end
def run(x)
calculate x
output $h
end
end
if $0 == __FILE__
n = 1.36
ChangeCalculator.new.run(n)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment