Created
January 27, 2012 16:43
-
-
Save dmerrick/1689685 to your computer and use it in GitHub Desktop.
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
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