Skip to content

Instantly share code, notes, and snippets.

@wlodi83
Created December 8, 2012 16:01
Show Gist options
  • Save wlodi83/4240836 to your computer and use it in GitHub Desktop.
Save wlodi83/4240836 to your computer and use it in GitHub Desktop.
greedy algorithm v2
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
if number >= cents.first
number.div(cents.first).times { rest << cents.first }
number -= (cents.first * number.div(cents.first))
cents.shift
else
cents.shift
end
end
print rest.flatten
end
make_change(1000001, [1000000, 1])
make_change(39)
make_change(14, [10, 7, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment