Created
October 9, 2009 18:00
-
-
Save shayarnett/206210 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 count_change(amount, denominations = [50,25,10,5,1]) | |
result = 1 | |
largest_denomination_count = 1 | |
until denominations.size == 1 | |
until makes_change?(amount, denominations, largest_denomination_count) == false | |
puts "change made with #{largest_denomination_count} coins of value #{denominations.first}" | |
result += 1 | |
largest_denomination_count += 1 | |
end | |
largest_denomination_count = 1 | |
denominations.slice!(0) | |
end | |
puts "#{result} possible combinations for change of #{amount}" | |
end | |
def makes_change?(amount, denominations, largest_denomination_count) | |
(denominations.first * largest_denomination_count) <= amount | |
end | |
count_change(100) | |
# problem is this only counts 1 combination as it decrements :( | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment