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