Created
December 8, 2012 16:38
-
-
Save wlodi83/4240921 to your computer and use it in GitHub Desktop.
greedy algorithm v3 (big amount of coins)
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.sort!.reverse | |
rest = [] | |
while number > 0 | |
el = cents.select {|x| (number % x) == 0} | |
number.div(el.first.to_i).times { rest << el.first } | |
number -= el.first.to_i * number.div(el.first.to_i) | |
cents.pop(el.first) | |
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