Last active
December 15, 2015 11:09
-
-
Save gaahrdner/5251173 to your computer and use it in GitHub Desktop.
Ruby Quiz #154
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
#!/usr/bin/env ruby | |
coins = { | |
:penny => { :value => 0.01, :count => 0 }, | |
:nickel => { :value => 0.05, :count => 0 }, | |
:dime => { :value => 0.1, :count => 0 }, | |
:quarter => { :value => 0.25, :count => 0 } | |
} | |
amount = ARGV[0].to_f.round(2) | |
coins.sort_by { |k,v| v[:value] }.reverse.each do |coin, value| | |
coins[coin][:count] = (amount/value[:value]).to_i | |
amount = (amount - (value[:value] * value[:count])).to_f.round(2) | |
end | |
puts coins | |
#╭─gaahrdner@nipsey /tmp ‹1.9.3-p327› | |
#╰─$ ./ruby_quiz_154.rb 1.45 | |
#{:penny=>{:value=>0.01, :count=>0}, :nickel=>{:value=>0.05, :count=>0}, :dime=>{:value=>0.1, :count=>2}, :quarter=>{:value=>0.25, :count=>5}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rounding :-/