Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active August 29, 2015 14:23
Show Gist options
  • Save MaxPleaner/b6aaba633ed3fbcbbbe5 to your computer and use it in GitHub Desktop.
Save MaxPleaner/b6aaba633ed3fbcbbbe5 to your computer and use it in GitHub Desktop.
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)
@MaxPleaner
Copy link
Author

Uploaded from the commad line with defunkt.io/gist/

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