Need help with knapsack style problem please. Thanks

Ok guys so i am trying to create 2 versions of a program which sorts items into boxes. The first version i have to create puts the item weights (integer between 1 and 20) into the first "box" they will fit into. So given the input 11,17,3,5,4 the list the program would out put would be 19,17,4. This version i have working fine.

The next version has to put the integers into the boxes in which they will fit best. The code is exactly the same as my first version except i have changed the function which calculates which list item to add the weight to. Basically i have changed the function however is still gives the same output as the first version, i know its somewhere in the function im going wrong, just hoping a fresh pair of eyes could give it the once over and help me out, thanks a million.

I can not see that you changed the function, you are putting value to first fitting slot from beginning, not say find combinations closest to container size. To inputs look ugly, you can use while itemweight: and read the values to list from input inside the while. Put some print statements around the program to see what happens or maybe better to start over and add bit by bit the lines with testing. Probably you should write down your logic and write tests to proove that your program works before you start.

I can not see that you changed the function, you are putting value to first fitting slot from beginning, not say find combinations closest to container size. To inputs look ugly, you can use while itemweight: and read the values to list from input inside the while. Put some print statements around the program to see what happens or maybe better to start over and add bit by bit the lines with testing. Probably you should write down your logic and write tests to proove that your program works before you start.

I know that there is probably better more efficient ways to do this but my task document specifies that i create a function which returns a value, which tells the program where to put the integer and -1 if it doesn't fit anywhere.
It then says that i should only need to change the function to get the program to fit the items more efficiently.

You could find at least for value i in values, if there is 20-value and for each container with values, if value 20-total_of_container. There is advanced ways, but the problem is tough one (so called np-hard problem). Generally you could use itertools.combinations and test the sums.

You could find at least for value i in values, if there is 20-value and for each container with values, if value 20-total_of_container. There is advanced ways, but the problem is tough one (so called np-hard problem). Generally you could use itertools.combinations and test the sums.

Wouldnt that mean that if none of the containers values go up to 20 it wouldnt return a value

EDIT: The first time it is assigned a value is when it takes in an input from the user

You must then minimize the difference. The other alternative is trying to equalize the total of all containers. It depends how you define 'fits best'. Minimizing the waste space is generally used definition though.

For this test case is enough then that you put 12,15 to container one and two and when read 5 see that two is missing 5 and put it ther and same for 12 and 8 etc. However consider that result should work if we enter one more input say 10 and it should not put 9 to number three.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...