Skip to content

Instantly share code, notes, and snippets.

@wlodi83
Created December 8, 2012 16:38
Show Gist options
  • Save wlodi83/4240921 to your computer and use it in GitHub Desktop.
Save wlodi83/4240921 to your computer and use it in GitHub Desktop.
greedy algorithm v3 (big amount of coins)
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