Created
December 7, 2012 17:03
-
-
Save wlodi83/4234699 to your computer and use it in GitHub Desktop.
greedy algorithm in ruby
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
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