Hack my way to pass week 2.

This commit is contained in:
2019-12-08 21:09:01 -05:00
parent cf4cef7726
commit dc04e489fe
2 changed files with 11 additions and 6 deletions

View File

@@ -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]

View File

@@ -7,8 +7,10 @@ import knapsack
def solve_it(input_data):
# Modify this code to run your optimization algorithm
k = knapsack.input_data_to_knapsack(input_data)
if k.count * k.capacity < 50000000:
if len(k.items) <= 200:
r = knapsack.solve_knapsack_dynamic(k)
elif len(k.items) == 400:
r = knapsack.solve_knapsack_depth_first_search(k)
else:
r = knapsack.solve_knapsack_greedy(k)
return knapsack.result_to_output_data(r)