Skip to content

Instantly share code, notes, and snippets.

@mariapacana
Created May 11, 2013 01:55
Show Gist options
  • Save mariapacana/5558619 to your computer and use it in GitHub Desktop.
Save mariapacana/5558619 to your computer and use it in GitHub Desktop.
second knapsack
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