Created
May 11, 2013 01:55
-
-
Save mariapacana/5558619 to your computer and use it in GitHub Desktop.
second knapsack
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 knapsack2(menu, target, steps, next_step) | |
size = menu.size | |
current_price = (0...size).inject(0) {|r, i| r + menu[i]*steps[i]} | |
# File.open("knapsack.txt", 'w') {|f| f.write("menu = #{menu}, target = #{target}, current_price = #{current_price}" + "steps = #{steps}") } | |
if current_price == target | |
return steps | |
elsif current_price > target | |
else | |
(0...size).each do |i| | |
next_step = Array.new(size, 0) | |
next_step[i] = 1 | |
steps = add_arrays(steps, next_step) | |
File.open("knapsack.txt", 'a+') {|f| f.write("steps = #{steps}\n").inspect } | |
knapsack2(menu, target, steps) | |
end | |
end | |
end | |
puts knapsack2(menu_1, target_1, [0, 0, 0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment