After learning a few of the simple area's in python, such as lists, I decided that I might try and replicate the algorithms that we had been learning in Maths. There is a part of the course called D1 (Decision Maths One) which is all about Algorithms and Graphs (Not the Bar Type but the Nodes and Vertices Type). The first few algorithms you learn about are called number sorting and bin packing algorithms. Bubble Sort and Quick Sort can be used to sort a list of numbers into ascending or descending order, Bin Packing can be used to calculate the most efficient way of fitting all the packages you have into all of the bins you have. Algorithms are specifically designed for computers; humans have common sense so they can clearly tell what the lowest number in the list is just by looking at it. However a computer cannot and thus must sort through the list logically and systematically to get to the correct answer. For a six number list this seems like a waste of time, but if your list is 1000 numbers long and the numbers have seven digits each, then the computer is going to do it faster. This is the same with Bin Packing, it can be useful for the postal services to see what packages to put in each van, for example.
I originally decided to try and replicate these algorithms to the letter, and with the Bin Packing Algorithm I was rather successful. This program uses a series of "compare" commands to see if the total height of the packages in the bin plus the height of the one you are currently trying to add exceeds the total height of the bin. If it doesn't then that package is put in that bin, if it does then the program moves on to check on the next bin, etc. When it is done it shows you a list of the bin configurations and if any of the packages did not fit.
If you want to try my programs for yourself then download the latest version of Python 3, for windows users download the Python 3.4.2 x86 MSI Installer:
Number Sort:
a = 20
b = 16
c = 4
d = 7
e = 2
f = 1
t = 1
BubbleList = [a,b,c,d,e,f]
print(BubbleList)
while t > 0:
while BubbleList[0] > BubbleList[1]:
BubbleList.append(BubbleList[0])
BubbleList.remove(BubbleList[0])
while BubbleList[1] > BubbleList[2]:
BubbleList.append(BubbleList[1])
BubbleList.remove(BubbleList[1])
while BubbleList[2] > BubbleList[3]:
BubbleList.append(BubbleList[2])
BubbleList.remove(BubbleList[2])
while BubbleList[3] > BubbleList[4]:
BubbleList.append(BubbleList[3])
BubbleList.remove(BubbleList[3])
if BubbleList[4] > BubbleList[5]:
BubbleList.append(BubbleList[4])
BubbleList.remove(BubbleList[4])
print(BubbleList)
if BubbleList[0] - BubbleList[1] < 0:
if BubbleList[1] - BubbleList[2] < 0:
if BubbleList[2] - BubbleList[3] < 0:
if BubbleList[3] - BubbleList[4] < 0:
if BubbleList[4] - BubbleList[5] < 0:
t = 0
print(BubbleList)
print("Complete!")
Bin Packing:
print("Please Enter your Bin Size, Amount of Bins and Package Sizes!")
print("Remember, there is a max of six bins!")
print("Lets Pack!")
print("")
print("")
BinOne = [0]
BinTwo = [0]
BinThree = [0]
BinFour = [0]
BinFive = [0]
BinSix = [0]
NoComply = [0]
a = 0.5
b = 0.8
c = 1.1
d = 0.9
e = 0.4
f = 0.7
g = 1.2
h = 6
i = 0
j = 1
if a <= g:
BinOne.append(a)
else:
NoComply.append(a)
if b + sum(BinOne) <= g:
BinOne.append(b)
elif b <= g:
BinTwo.append(b)
else:
NoComply.append(b)
if c + sum(BinOne) <= g:
BinOne.append(c)
elif c + sum(BinTwo) <= g:
BinTwo.append(c)
elif c <= g:
BinThree.append(c)
else:
NoComply.append(c)
if d + sum(BinOne) <= g:
BinOne.append(d)
elif d + sum(BinTwo) <= g:
BinTwo.append(d)
elif d + sum(BinThree) <= g:
BinThree.append(d)
elif d <= g:
BinFour.append(d)
else:
NoComply.append(d)
if e + sum(BinOne) <= g:
BinOne.append(e)
elif e + sum(BinTwo) <= g:
BinTwo.append(e)
elif e + sum(BinThree) <= g:
BinThree.append(e)
elif e + sum(BinFour) <= g:
BinFour.append(e)
elif e <= g:
BinFive.append(e)
else:
NoComply.append(e)
if f + sum(BinOne) <= g:
BinOne.append(f)
elif f + sum(BinTwo) <= g:
BinTwo.append(f)
elif f + sum(BinThree) <= g:
BinThree.append(f)
elif f + sum(BinFour) <= g:
BinFour.append(f)
elif f + sum(BinFive) <= g:
BinFive.append(f)
elif f <= g:
BinSix.append(f)
else:
NoComply.append(f)
BinOne.remove(BinOne[0])
BinTwo.remove(BinTwo[0])
BinThree.remove(BinThree[0])
BinFour.remove(BinFour[0])
BinFive.remove(BinFive[0])
BinSix.remove(BinSix[0])
NoComply.remove(NoComply[0])
PrintList = ["BinOne:",BinOne, "BinTwo:",BinTwo, "BinThree:",BinThree, "BinFour:",BinFour, "BinFive:",BinFive, "BinSix:",BinSix]
print("NoComply")
print(NoComply)
while j < h+1:
print(PrintList[i])
print(PrintList[i+1])
i=i+2
j=j+1
Happy Programming!
P.S I am going to leave an opinion Poll below to see what content you guys would like to see more of. More Programming? More Robot Arm? More RC Planes? It would be much appreciated if you could cast your vote below!