- 2

Why🥺🥺🥺why don't get to output....why

def solve_knapsack(profits, weights, capacity): # basic checks n = len(profits) if capacity <= 0 or n == 0 or len(weights) != n: return 0 dp = [[0 for x in range(capacity+1)] for y in range(n)] # populate the capacity = 0 columns, with '0' capacity we have '0' profit for i in range(0, n): dp[i][0] = 0 # if we have only one weight, we will take it if it is not more than the capacity for c in range(0, capacity+1): if weights[0] <= c: dp[0][c] = profits[0] # process all sub-arrays for all the capacities for i in range(1, n): for c in range(1, capacity+1): profit1, profit2 = 0, 0 # include the item, if it is not more than the capacity if weights[i] <= c: profit1 = profits[i] + dp[i - 1][c - weights[i]] # exclude the item profit2 = dp[i - 1][c] # take maximum dp[i][c] = max(profit1, profit2) print_selected_elements(dp, weights, profits, capacity) # maximum profit will be at the bottom-right corner.

18th Jan 2022, 7:39 PM
Bhebheko1125 Lonia
4 Answers
+ 1
Share a link to saved code bit rather than pasting raw text code. Big code can't fit in, and as you can see, it had to be broken into chunks making it difficult for others to test the code. https://www.sololearn.com/post/75089/?ref=app And please use appropriate tags. 'hooo' doesn't help describe anything ☝ https://code.sololearn.com/W3uiji9X28C1/?ref=app
18th Jan 2022, 10:54 PM
Ipang
0
return dp[n - 1][capacity] def solve_knapsack(profits, weights, capacity): # basic checks n = len(profits) if capacity <= 0 or n == 0 or len(weights) != n: return 0 dp = [[0 for x in range(capacity+1)] for y in range(n)] # populate the capacity = 0 columns, with '0' capacity we have '0' profit for i in range(0, n): dp[i][0] = 0 # if we have only one weight, we will take it if it is not more than the capacity for c in range(0, capacity+1): if weights[0] <= c: dp[0][c] = profits[0] # process all sub-arrays for all the capacities for i in range(1, n): for c in range(1, capacity+1): profit1, profit2 = 0, 0 # include the item, if it is not more than the capacity if weights[i] <= c: profit1 = profits[i] + dp[i - 1][c - weights[i]] # exclude the item profit2 = dp[i - 1][c] # take maximum dp[i][c] = max(profit1, profit2) print_selected_elements(dp, weights, profits, capacity) # maximum profit will be at the bottom-
18th Jan 2022, 7:40 PM
Bhebheko1125 Lonia
0
return dp[n - 1][capacity]str(solve_knapsack([1, 6, 10, 16], [1, 2, 3, 5], 7))) print(' ') print("B. Total knapsack profit: " + str(solve_knapsack([1, 6, 10, 16], [1, 2, 3, 5], 6))) print("C. Total knapsack profit: " + str(solve_knapsack([1, 6, 10, 16], [1, 2, 3, 5], 5))) main()
18th Jan 2022, 7:42 PM
Bhebheko1125 Lonia
0
dp = [[0 for x in range(capacity+1)] for y in range(n)] # populate the capacity = 0 columns, with '0' capacity we have '0' profit for i in range(0, n): dp[i][0] = 0 # if we have only one weight, we will take it if it is not more than the capacity for c in range(0, capacity+1): if weights[0] <= c: dp[0][c] = profits[0] # process all sub-arrays for all the capacities for i in range(1, n): for c in range(1, capacity+1): profit1, profit2 = 0, 0 # include the item, if it is not more than the capacity if weights[i] <= c: profit1 = profits[i] + dp[i - 1][c - weights[i]] # exclude the item profit2 = dp[i - 1][c] # take maximum dp[i][c] = max(profit1, profit2) print_selected_elements(dp, weights, profits, capacity) # maximum profit will be at the bottom-right corner. return dp[n - 1][capacity] def print_selected_elements(dp, weights, profits, capacity): print("Selected weights are: ", end='') n = len(weights) totalProfit = dp[n-1][capacity] for i in range(n-1, 0, -1):
18th Jan 2022, 7:42 PM
Bhebheko1125 Lonia