My problem is that i have to create a program to hold a series of weights in containers (elements in a list). The max weight for each container is 20units. I have got this working as this:

containers = [0]
inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")

def findContainers(containers,inp):

for i in range(0,len(containers)):

if inp + containers <= 20:
return i

elif inp + containers[len(containers)-1] > 20:
return -1


def Allocator(containers,inp):

x = 0
while inp != 0:
i = findContainers(containers,inp)

if i == -1:
containers = containers + [inp]
i = len(containers) - 1

else:
containers = containers + inp

x = x + 1
print "Item ", x , ", Weight ", inp ,": Container ", (i + 1)
inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")


Allocator(containers,inp)


However, i need the weights to store themselves in the fullest container that can hold them.

E.g. If i had two contaainers:
1. 17
2. 18

and i wanted to add a weight of 2, whilst the first one could hold it, the weight will go on to the second container, and make it complete.

Thanks in advance for all help

Please repost with code tags (push the code button before you paste!). Then we can read your code much easier.

Oh sorry, the code is:

containers = [0]
inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")

def findContainers(containers,inp):
    
    for i in range(0,len(containers)):
        
        if inp + containers[i] <= 20:
            return i
        
        elif inp + containers[len(containers)-1] > 20:
            return -1


def Allocator(containers,inp):
    
    x = 0
    while inp != 0:
        i = findContainers(containers,inp)
        
        if i == -1:
            containers = containers + [inp]
            i = len(containers) - 1
            
        else:
            containers[i] = containers[i] + inp
            
        x = x + 1
        print "Item ", x , ", Weight ", inp ,": Container ", (i + 1)
        inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")
    print "Container weights: ", containers    



Allocator(containers,inp)

Basically, this version will give the the output:
"Container weights: [17, 15, 18, 9]" with the inputs 12, 15, 5, 10, 8, 9

I want it to output:
"Container weights: [20, 20, 19]" with the same input.

Thanks again

Oh sorry, the code is:

containers = [0]
inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")

def findContainers(containers,inp):
    
    for i in range(0,len(containers)):
        
        if inp + containers[i] <= 20:
            return i
        
        elif inp + containers[len(containers)-1] > 20:
            return -1


def Allocator(containers,inp):
    
    x = 0
    while inp != 0:
        i = findContainers(containers,inp)
        
        if i == -1:
            containers = containers + [inp]
            i = len(containers) - 1
            
        else:
            containers[i] = containers[i] + inp
            
        x = x + 1
        print "Item ", x , ", Weight ", inp ,": Container ", (i + 1)
        inp = input("Enter a weight, 20 or less, or enter 0 to quit: ")
    print "Container weights: ", containers    



Allocator(containers,inp)

Basically, this version will give the the output:
"Container weights: [17, 15, 18, 9]" with the inputs 12, 15, 5, 10, 8, 9

I want it to output:
"Container weights: [20, 20, 19]" with the same input.

Thanks again

Here is a way

>>> containers = [0]
>>> def find(w):
...  try:
...   w, i = max((x, i) for i, x in enumerate(containers) if x + w <= 20)
...   return i
...  except ValueError:
...   containers.append(0)
...   return len(containers) - 1
... 
>>> def process(seq):
...  for w in seq:
...   i = find(w)
...   containers[i] += w
... 
>>> process([12, 15, 5, 10, 8, 9])
>>> print containers
[20, 20, 19]

You should add a test to reject inputs > 20.

I don't understand this, i think all i need is an IF statement in the findContainers function, can anyone do it this way, i have already tried with the max function, can't seem to get the wording right in the IF statement

Hey guys thanks for your help, jst solved it, all it was, was an IF statement with an AND included.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.