stepHeight = 30 shelfDepth = 400 verbose = True def findIdealHeightVert(boxHeight, ideal, starting): bestFit = starting prev = 9999 running = True while running: currentErr = ideal - (boxHeight - (bestFit * stepHeight)) if abs(currentErr) < abs(prev): bestFit +=1 prev = currentErr else: running = False bestFit -=1 return bestFit, prev def findIdealHeightStack(boxHeight, ideal): stack = 0 prev = 9999 bestFit = 0 fit = [] error = [] for x in xrange(1, 11): currentFit, currentErr = findIdealHeightVert((boxHeight*x), ideal, 0) #perhaps should use currentFit to save some calculations fit.append(currentFit) error.append(currentErr) if abs(currentErr) < abs(prev): prev = currentErr stack = x bestFit = currentFit return stack, bestFit, prev, fit, error def findBestFitVert(ideal,boxes): pos = 0 prev = 9999 pegs = 0 orientation = "none" globalfit = [] for x in xrange(0, len(boxes)): if boxes[x][2] < shelfDepth: currpegs, currentErr = findIdealHeightVert(boxes[x][1], ideal, 0) if verbose: globalfit.append([boxes[x][0],"X",currpegs,currentErr]) #print boxes[x][0][:23] + "\t X \tPegs:" + str(currpegs) + "\tError:" + str(currentErr) + "mm" if abs(currentErr) < abs(prev): prev = currentErr pos = x pegs = currpegs orientation = "X" for x in xrange(0, len(boxes)-1): if boxes[x][1] < shelfDepth: currpegs, currentErr = findIdealHeightVert(boxes[x][2], ideal, 0) if verbose: globalfit.append([boxes[x][0],"Y",currpegs,currentErr]) #print boxes[x][0][:23] + "\t Y \tPegs:" + str(currpegs) + "\tError:" + str(currentErr) + "mm" if abs(currentErr) < abs(prev): prev = currentErr pos = x pegs = currpegs orientation = "Y" globalfit.sort(key=lambda x: abs(x[3])) return globalfit def findBestFitFlat(ideal, boxes): pos = 0 prev = 9999 pegs = 0 stack = 0 globalfit = [] for x in xrange(0, len(boxes)): stacklist = [1,2,3,4,5,6,7,8,9,10] if boxes[x][2] < shelfDepth or boxes[x][1] < shelfDepth: currstack, currpegs, currentErr, fit, error = findIdealHeightStack(boxes[x][3], ideal) if verbose: for y in xrange(0, len(fit)): globalfit.append([boxes[x][0],stacklist[y],fit[y],error[y]]) if abs(currentErr) < prev: prev = abs(currentErr) pos = x pegs = currpegs stack = currstack globalfit.sort(key=lambda x: abs(x[3])) return globalfit f = open('output.txt', 'w') print "\nCalculating Lord Of The Rings waterline...\n" boxes = [["9469-1: Gandalf Arrives",191,141,46], ["9471-1: Uruk-Hai Army",282,262,64], ["9474-1: The Battle Of Helm's Deep",582,378,87], ["9470-1: Shelob Attacks",262,191,61], ["9472-1: Attack On Weathertop",480,282,61], ["9473-1: The Mines of Moria",480,378,71], ["9476-1: The Orc Forge",380,260,54]] results = findBestFitFlat(191,boxes) f.write("Lord Of The Rings\n\n") for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" boxes = [ ["Any Hero Factory style model",141,262,48] , ["6858-1: Catwoman Catcycle City Chase",191,141,46] , ["6860-1: The Batcave",480,378,71] , ["6862-2: Superman Vs Power Armor Lex",191,262,61] , ["6863-1: Batwing Battle Over Gotham City",382,262,56] , ["6864-1: The Batmobile and the Two-Face Chase",480,282,61] , ["6857-1 Dynamic Duo Funhouse Escape",380,260,54]] print "\nCalculating DC Super Heroes waterline...\n" f.write("\nDC Super Heroes\n\n") results = findBestFitFlat(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" boxes = [ ["Any Hero Factory style model",141,262,48] , ["6866-1 or 6867-1: Wolverine/Loki",262,191,61] , ["6868-1: Hulk's Helicarrier Breakout",480,282,61] , ["6865-1: Captain America's Avenging Cycle",191,141,46] , ["6869-1: Quinjet Aerial Battle",480,378,71] , ["6873-1: Spider-Man's Doc Ock Ambush",380,260,54] ] print "\nCalculating Marvel Super Heroes waterline...\n" f.write("\nMarvel Super Heroes\n\n") results = findBestFitFlat(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "\nCalculating Chima waterline...\n" boxes = [["70000 Razcals Glider",141,122,46], ["70001 Crawleys Claw Ripper",262,141,48], ["70002/70115 Lennox Lion Attack",262,191,60], ["70003 Eris Eagle Interceptor",282,262,64], ["70004 Wakz'Pack Tracker",282,262,64], ["70005 Lavals Royal Fighter",382,262,71], ["70006 Craggers Command Ship",480,378,70.5], ["70011 Eagles Castle",282,262,76], ["70012 Razars CHI Raider",382,262,56], ["70013 Equilas Ultra Striker",382,262,56]] results = findBestFitFlat(191,boxes) f.write("\nChima\n\n") for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "\nCalculating Friends (new) waterline...\n" boxes = [["New 4.99 41000",122,91,47], ["New 9.99 41003 41011",157,141,45], ["New 29.99 41007",282,262,64], ["New 17.99 41004",262,191,61], ["New 39.99 41008",382,262,71]] results = findBestFitFlat(191,boxes) f.write("\nFriends (new)\n\n") for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "\nCalculating Friends(old) waterline...\n" boxes = [["3189",540,282,59], ["Old 4.99",122,91,47], ["3188",480,282,61], ["3315",480,378,70], ["Old 9.99",191,141,46], ["Old 19.99",354,191,59], ["Old 29.99",382,262,56], ["3183",262,141,48], ["3061",382,262,56]] results = findBestFitFlat(191,boxes) f.write("\nFriends (old)\n\n") for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "\nCalculating Technic waterline...\n" boxes = [["42000-1: Grand Prix Racer",475,377,91],["42010-1: Off-road Racer",261,140,60],["42007-1: Moto Cross Bike",352,190,56],["42006-1: Excavator",350,378,66],["42004-1: Mini Backhoe Loader",261,189,60],["42011-1: Race Car",261,140,46],["42002-1: Hovercraft",261,140,46],["9395-1: Pick-Up Tow Truck",480,282,74],["9394-1: Jet Plane",382,262,71],["9392-1: Quad Bike",354,191,59],["9391-1: Tracked Crane",262,191,46],["9390-1: Mini Tow Truck",191,141,61]] ##4x4crawler logging truck helicopter unimog results = findBestFitFlat(191,boxes) f.write("\nFriends (old)\n\n") for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Stack: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" print "" f.write("\n") results = findBestFitVert(191,boxes) for x in xrange(0, len(results)): f.write(str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm\n") print str(results[x][0])[:23] + "\t Orientation: " + str(results[x][1])[:23] + "\t Pegs: " + str(results[x][2])[:23] + "\t Error: " + str(results[x][3])[:23] + "mm" f.close() ##for x in xrange(0, len(boxesName)): ## if x > 0: ## print ",", ## else: ## print "boxes = [", ## print '["' + boxesName[x] + '",' + str(boxesX[x]) + ',' + str(boxesY[x]) + ',' + str(boxesZ[x]) + ']', ## if x = len(boxesName): ## print "]"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question