Skip to content

Instantly share code, notes, and snippets.

@gaahrdner
Last active December 15, 2015 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaahrdner/5251173 to your computer and use it in GitHub Desktop.
Save gaahrdner/5251173 to your computer and use it in GitHub Desktop.
Ruby Quiz #154
#!/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}}
@gaahrdner
Copy link
Author

rounding :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment