
Patrick B. answered 07/06/21
Math and computer tutor/teacher
class Box:
def __init__(self,l,w):
self.length = l
self.width = w
def GetLength(self):
return(self.length)
def GetWidth(self):
return(self.width)
def GetArea(self):
return(float(self.length)*float(self.width))
class Bin:
def __init__(self,A):
self.area = A
self.boxes = []
def GetArea(self):
return(self.area)
def PushBox(self,box):
self.boxes.append(box)
self.area = self.area - box.GetArea()
def GetBoxes(self):
return(self.boxes)
#--------------------------------------------------------
# Selection sort: sorts the boxes by area
def Sort(boxes):
N = len(boxes)
for i in range(N):
startingBox = boxes[i]
minArea = startingBox.GetArea()
indexOfMin = i
for j in range(i,N):
curBox = boxes[j]
curArea = curBox.GetArea()
if float(curArea) < float(minArea):
minArea = curArea
indexOfMin = j
tempBox = boxes[i]
boxes[i] = boxes[indexOfMin]
boxes[indexOfMin] = tempBox
#-----------------------------------------------------
def DumpBoxes(boxes):
N = len(boxes)
if (N>0):
#print(" DumpBoxes : # of boxes = " + str(N))
for i in range(int(N)):
print(" box # " + str(i+1))
curBox = boxes[i]
print(" length = " + str(curBox.GetLength()))
print(" width = " + str(curBox.GetWidth()))
print(" area = " + str(curBox.GetArea()))
#--------------------------------------------------------------
# 25 bins with areas:
# 100,100,50,50,50,25,25,25,25,10,10,10,10,10,
# 5,5,5,5,5,5,1,1,1,1,1
#---------------------------------------------------
bins = []
for i in range(2):
bins.append(Bin(100))
for i in range(3):
bins.append(Bin(50))
for i in range(4):
bins.append(Bin(25))
for i in range(5):
bins.append(Bin(10))
for i in range(6):
bins.append(Bin(5))
for i in range(5):
bins.append(Bin(1))
""" #DEBUG
for i in range(20):
curBin = bins[i]
print(curBin.GetArea())
"""
boxes = []
numBoxes=0
while int(numBoxes)<1:
numBoxes = input("How many boxes ??? :>")
for i in range(int(numBoxes)):
print(" box # " + str(i+1))
curLength = input("input length :>")
curWidth = input("input width :>")
curBox = Box(curLength,curWidth)
boxes.append(curBox)
Sort(boxes)
#DumpBoxes(boxes)
#We will ASSUME that there is enough total area in the bins
for boxLoop in range(int(numBoxes)):
curBox = boxes[boxLoop]
curBoxArea = curBox.GetArea()
# print(" current box area = " + str(curBoxArea))
for binLoop in range(19,-1,-1):
curBin = bins[binLoop]
curBinArea = curBin.GetArea()
if (curBinArea > curBoxArea):
# print(" bin # " + str(binLoop+1) + " fits ")
curBin.PushBox(curBox)
break
for binLoop in range(20):
curBin = bins[binLoop]
print(" Bin # " + str(binLoop+1))
curBoxList = curBin.GetBoxes()
DumpBoxes(curBoxList)
------------------------------------------------
Sorry, but I only speak object oriented.
bins are linear searched from smallest to largest.
Input 3 boxes: 11x8=88, 6x4=24, 12x5=60
box array gets sorted: 24,60,88
starting with smallest bin at index 19, the first available bin that fits is of area 25.
then the 2nd bin of 100 shall accommodate the box of area 60. Finally the first
box of area 100 shall take the box of size 88, since the 2nd box of area 100 now
only has 100-60 = 40 square units of area available
the output shows bin #1 contains the box of area 88, the 2nd bin contains the box of area 60,
and the box of area 24 in the last box of area 25