Brittney P.

# Python: First Fit Algorithm

I'm trying to create a First Fit Algorithm. The approach I'm taking is creating a list of empty lists, which are representative of the bins, where they will then be filled by certain area values that add up to the bin area. I want that to be continued until most of the areas can be filled.

This is where my problem arises:

lists.append([])

for i in lists:

for box in boxes:

l = box[0]

w = box[1]

area = l * w

if area <= bin_area:

bin_area = bin_area - area

lists[0].append(area)

else:

bin_area = 15

if area <= bin_area:

bin_area = bin_area - area

lists[1].append(area)

**This is where I want to then create a new list empty list where I can add more values that add up to the bin value. I tried, by guessing, lists[ i ].append([area]), but the indices has to be integer.

How do I accomplish this? Any help would be appreciated!

Also, here is my full code:

def FirstFitAlg():

box1 = (3,2)

box2 = (1,4)

box3 = (2,1)

box4 = (4,3)

box5 = (1,2)

boxes = [box1,box2,box3,box4,box5]

num_of_boxes = len(boxes)

bin_area = 15

n_bin = 0

lists = []

lists.append([])

lists.append([])

#for i in lists:

for box in boxes:

l = box[0]

w = box[1]

area = l * w

if area <= bin_area:

bin_area = bin_area - area

lists[0].append(area)

else:

bin_area = 15

if area <= bin_area:

bin_area = bin_area - area

lists[1].append(area)

#Once it fills the second list, I want it to create a new list that holds the new values

#My issue is that lists[i].append() does not work, since the indices has to be a integer

print(lists)

for i in lists:

if len(i) >= 1:

n_bin += 1

print(n_bin)

efficiency = (n_bin/num_of_boxes) * 100

print(efficiency)

By:

Tutor
4.7 (31)

Math and computer tutor/teacher

## Still looking for help? Get the right answer, fast.

Get a free answer to a quick problem.
Most questions answered within 4 hours.

#### OR

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.