Why isn't 1 used as x in the first for loop


#1

Why isn’t the for loop iterating through 1?


#2

We will have a much easier time with this question if you post raw code, not just a picture.


#3

def full_bin(list_in, max):
    count = -1
    left = list_in
    bin_f = []
    for x in list_in:
        print x
        ind_x = list_in.index(x)
        stop = 0
        for y in list_in[ind_x + 1:]:
            print "(%d)" % (y)
            if stop == 1:
                pass
            elif x + y == max:
                bin_f.append([])
                count += 1
                bin_f[count].append(left.pop(left.index(x)))
                bin_f[count].append(left.pop(left.index(y)))
                stop  = 1
    return bin_f

print full_bin([3, 2, 1, 5, 4], 5)

#4

what it prints out:
3
(2)
(1)
(5)
(4)
5
(4)
4
[[3, 2]]


#5

What is the objective of the program? To return a list of addends that sum to max?


#6

Its the full bin packing algorithm. Its where you have list of objects of different sizes and you have to fit it into different bins with a max size.


#7

So you want output like,

[[3,2], [1,4], [5]]

??


#8

This part of the code is pairing up two numbers in the list that have a total sum equal to the maximum size.


#9

yeah. The lists within the lists represent the different bins


#10

here’s another failed attempt that kind of worked.

The part where it stops working is because some numbers are not iterated through the for loop.

Btw the first function is a different algorithm that does the same thing but does not lead to the best solution. It has to be used for this algorithm to be completed.


#11

Can we assume that there are no values in the list greater than max?

If so, then,

>>> def full_bin(x, max):
    bin_f = []
    temp = sorted(x)
    while len(temp) > 0:
        if temp[0] + temp[-1] == max:
            bin_f.append([temp.pop(0), temp.pop(-1)])
        elif temp[-1] == max:
            bin_f.append([temp.pop(-1)])
    return bin_f

>>> n = [3,2,1,5,4]
>>> print (full_bin(n,5))
[[5], [1, 4], [2, 3]]
>>> 

#12

Thanks for taking the time to help me out. Really appreciate it.