This code aims to take an array of integer coin values, an amount of change to be made, and an empty array to pass around for collecting the answer, and should return a set of coins that makes the exact right amount of change (assumes that the problem is possible).
From the puts statement, it seems that the recursion seems to be behaving well and seeking the right answer, but the returned solution isn't a valid answer. Could someone let me know what I'm doing wrong?
My code:
def brute_solver(coins,amount,answer)
puts "Coins: #{coins}, Amount: #{amount}, Answer: #{answer}"
return answer.sort! if amount == 0
coins.each do |c|
brute_solver(coins, amount - c, answer.dup << c) unless amount - c < 0
end
end
Sample attempt at running brute_solver:
1.9.3p194 :001 > require './change_challenge.rb'
=> true
1.9.3p194 :002 > brute_solver([1,3,5],7,[])
Coins: [1, 3, 5], Amount: 7, Answer: []
Coins: [1, 3, 5], Amount: 6, Answer: [1]
Coins: [1, 3, 5], Amount: 5, Answer: [1, 1]
Coins: [1, 3, 5], Amount: 4, Answer: [1, 1, 1]
Coins: [1, 3, 5], Amount: 3, Answer: [1, 1, 1, 1]
Coins: [1, 3, 5], Amount: 2, Answer: [1, 1, 1, 1, 1]
Coins: [1, 3, 5], Amount: 1, Answer: [1, 1, 1, 1, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 1, 1, 1, 1, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 1, 1, 1, 3]
Coins: [1, 3, 5], Amount: 1, Answer: [1, 1, 1, 3]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 1, 1, 3, 1]
Coins: [1, 3, 5], Amount: 2, Answer: [1, 1, 3]
Coins: [1, 3, 5], Amount: 1, Answer: [1, 1, 3, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 1, 3, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 1, 5]
Coins: [1, 3, 5], Amount: 3, Answer: [1, 3]
Coins: [1, 3, 5], Amount: 2, Answer: [1, 3, 1]
Coins: [1, 3, 5], Amount: 1, Answer: [1, 3, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 3, 1, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 3, 3]
Coins: [1, 3, 5], Amount: 1, Answer: [1, 5]
Coins: [1, 3, 5], Amount: 0, Answer: [1, 5, 1]
Coins: [1, 3, 5], Amount: 4, Answer: [3]
Coins: [1, 3, 5], Amount: 3, Answer: [3, 1]
Coins: [1, 3, 5], Amount: 2, Answer: [3, 1, 1]
Coins: [1, 3, 5], Amount: 1, Answer: [3, 1, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [3, 1, 1, 1, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [3, 1, 3]
Coins: [1, 3, 5], Amount: 1, Answer: [3, 3]
Coins: [1, 3, 5], Amount: 0, Answer: [3, 3, 1]
Coins: [1, 3, 5], Amount: 2, Answer: [5]
Coins: [1, 3, 5], Amount: 1, Answer: [5, 1]
Coins: [1, 3, 5], Amount: 0, Answer: [5, 1, 1]
=> [1, 3, 5]
eachreturns the array you asked it to iterate over. You're going to have to do something more to actually select the answer you're interested in. For instance, you might want to return whether any solutions exist. Or you might want to select the first solution found. Or you might want to select the "best" solution by some metric. You have to add logic to do one of these. - Jeremy Roman