Last active
August 29, 2015 14:23
-
-
Save MaxPleaner/b6aaba633ed3fbcbbbe5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def make_change(initial_cents=0) | |
remaining_cents = initial_cents | |
currency = { | |
# values are in num cents | |
"penny" => 1, | |
"nickel" => 5, | |
"dime" => 10, | |
"quarter" => 25, | |
"1 dollar" => 100, | |
"5 dollar" => 500, | |
"10 dollar" => 1000, | |
"20 dollar" => 2000, | |
"50 dollar" => 5000, | |
"100 dollar" => 10000 | |
} | |
change_made = [] | |
until remaining_cents == 0 | |
currency.reverse_each do |currency_name, currency_val| | |
# go through currency_values starting at largest | |
(remaining_cents / currency_val).times do | |
change_made << currency_name | |
remaining_cents -= currency_val | |
end | |
redo unless remaining_cents < currency_val | |
end | |
end | |
result = change_made.group_by { |x| x }.values.map do |group| | |
"#{group.length} #{group[0]}" | |
end.join(", ") | |
puts "making change from " + initial_cents.to_s + " cents: " + result.to_s | |
end | |
make_change(ARGV[0].to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uploaded from the commad line with defunkt.io/gist/