Hack my way to pass week 2.
This commit is contained in:
@@ -70,17 +70,19 @@ def solve_knapsack_depth_first_search(knapsack):
|
||||
|
||||
def get_max_value(from_index, capacity):
|
||||
value = 0
|
||||
for item in knapsack.items[from_index:]:
|
||||
items = knapsack.items
|
||||
|
||||
for i in range(from_index, num_items):
|
||||
item = knapsack.items[i]
|
||||
if item.weight <= capacity:
|
||||
value += item.value
|
||||
capacity -= item.weight
|
||||
else:
|
||||
value += int((item.weight / capacity) * item.value) + 1
|
||||
value += int((item.weight / knapsack.capacity) * item.value) + 1
|
||||
#value += int((item.weight / capacity) * item.value) + 1
|
||||
return value
|
||||
return value
|
||||
|
||||
return sum([i.value for i in knapsack.items[from_index:]])
|
||||
|
||||
def search(index, capacity, value, path, result):
|
||||
|
||||
if capacity < 0:
|
||||
@@ -91,7 +93,8 @@ def solve_knapsack_depth_first_search(knapsack):
|
||||
result["path"] = path
|
||||
|
||||
# Take current item.
|
||||
if value + get_max_value(index, capacity) > result["objective"]:
|
||||
max_value = get_max_value(index, capacity)
|
||||
if value + max_value > result["objective"]:
|
||||
item = knapsack.items[index]
|
||||
new_index = index + 1
|
||||
new_path = path + [1]
|
||||
|
||||
Reference in New Issue
Block a user