Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:35+00:00 2026-05-25T13:07:35+00:00

For an application I’m working on I need something like a packing algorithm implemented

  • 0

For an application I’m working on I need something like a packing algorithm implemented in Python see here for more details. The basic idea is that I have n objects of varying sizes that I need to fit into n bins, where the number of bins is limited and the size of both objects and bins is fixed. The objects / bins can be either 1d or 2d, interested in seeing both. (I think 3d objects is probably more than I need.)

I know there are a variety of algorithms out there that address this problem, such asBest Fit Decreasing and First Fit Decreasing, but I was hoping there might be an implementation in Python (or PHP/C++/Java, really I’m not that picky). Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:07:35+00:00Added an answer on May 25, 2026 at 1:07 pm

    https://bitbucket.org/kent37/python-tutor-samples/src/f657aeba5328/BinPacking.py

    """ Partition a list into sublists whose sums don't exceed a maximum 
        using a First Fit Decreasing algorithm. See
        http://www.ams.org/new-in-math/cover/bins1.html
        for a simple description of the method.
    """
    
    
    class Bin(object):
        """ Container for items that keeps a running sum """
        def __init__(self):
            self.items = []
            self.sum = 0
    
        def append(self, item):
            self.items.append(item)
            self.sum += item
    
        def __str__(self):
            """ Printable representation """
            return 'Bin(sum=%d, items=%s)' % (self.sum, str(self.items))
    
    
    def pack(values, maxValue):
        values = sorted(values, reverse=True)
        bins = []
    
        for item in values:
            # Try to fit item into a bin
            for bin in bins:
                if bin.sum + item <= maxValue:
                    #print 'Adding', item, 'to', bin
                    bin.append(item)
                    break
            else:
                # item didn't fit into any bin, start a new bin
                #print 'Making new bin for', item
                bin = Bin()
                bin.append(item)
                bins.append(bin)
    
        return bins
    
    
    if __name__ == '__main__':
        import random
    
        def packAndShow(aList, maxValue):
            """ Pack a list into bins and show the result """
            print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue, 'bins'
    
            bins = pack(aList, maxValue)
    
            print 'Solution using', len(bins), 'bins:'
            for bin in bins:
                print bin
    
            print
    
    
        aList = [10,9,8,7,6,5,4,3,2,1]
        packAndShow(aList, 11)
    
        aList = [ random.randint(1, 11) for i in range(100) ]
        packAndShow(aList, 11)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application : I am working on one mid-large size application which will be used
Application running in django, apache, python and mysql: I want to create global variable
Application is a C# .Net 3.5 WCF Service. I'd like during the build process
APPLICATION DESCRIPTION : I am a new iPhone developer. I am working on an
Application has many extension assemblies and they contain mappings for their classes. I need
Application able to record error in OnError, but we are not able to do
Application frameworks such as DotNetNuke, Eclipse, Websphere and so forth are available today which
Application has an auxiliary thread. This thread is not meant to run all the
Application 1 - Opens a SqlConnection and a SqlTransaction against a SQLServer 2005 database
Application is asp.net MVC. I want to put a textbox for date using mask.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.