educative.io

Code runs in on computer but not in browser

I have my code:

def knapsack(weights, prices, capacity):

n=len(weights)

your code goes here

if n==0:

 return 0

if capacity == 0:

return 0

t=[0]

for i in range(len(weights)):

if weights[i] <= capacity:

  t.append(knapsack( weights[0:i] + weights[(i+1): n   ],  prices[0:i] +  prices[(i+1): n ] , capacity-weights[i] ) + weights[i] )

return max(t)

stressTesting = False

Which runs on my computer just fine but all I get is Execution timed out! error

Hi,

Please note that different test cases are run on your code to check its correctness. It might be possible that your code took a lot of time while executing a test. Therefore, you got the Execution Timed Out! error.

One way to know about the test cases is that you run the code with the given correct solution. It will run successfully and will show all the instances on which the code was tested. You can get an idea about the test cases and try those test cases on your computer.

I hope it helps.

Can you change it so we can view what the test cases are? Otherwise I’m pretty stuck.

The are some of the test cases that you could find when you test the code:

  • Input: knapsack([1, 2, 4, 6],[4, 2, 4, 7],7) Output: 11
  • Input: knapsack([1, 2, 3, 5],[1, 6, 10, 16],6) Output: 17
  • Input: knapsack([2, 4, 6, 8],[11, 7, 9, 21],8) Output: 21
  • Input: knapsack([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],[13, 14, 12, 17, 11, 21, 23, 14, 10, 28, 10, 11, 31, 20],100) Output: 225
  • Input: knapsack([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99],[0, 1, 2, 9, 4, 25, 6, 49, 8, 81, 10, 121, 12, 169, 14, 225, 16, 289, 18, 361, 20, 441, 22, 529, 24, 625, 26, 729, 28, 841, 30, 961, 32, 1089, 34, 1225, 36, 1369, 38, 1521, 40, 1681, 42, 1849, 44, 2025, 46, 2209, 48, 2401, 50, 2601, 52, 2809, 54, 3025, 56, 3249, 58, 3481, 60, 3721, 62, 3969, 64, 4225, 66, 4489, 68, 4761, 70, 5041, 72, 5329, 74, 5625, 76, 5929, 78, 6241, 80, 6561, 82, 6889, 84, 7225, 86, 7569, 88, 7921, 90, 8281, 92, 8649, 94, 9025, 96, 9409, 98, 9801],4950) Output: 169100