Skip to content

Instantly share code, notes, and snippets.

@wlodi83
Created December 7, 2012 17:03
Show Gist options
  • Save wlodi83/4234699 to your computer and use it in GitHub Desktop.
Save wlodi83/4234699 to your computer and use it in GitHub Desktop.
greedy algorithm in ruby
def make_change(number, *coin_values)
coin_values.empty? ? cents = [50, 20, 10, 5, 2, 1] : cents = coin_values.flatten
rest = []
if number > 0
cents.each do |c|
while c <= number
number -= c
rest << c
end
end
else
puts "Negative value!"
end
print rest.flatten
end
make_change(14, [10, 7, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment