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

  • Home
  • SEARCH
  • 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 8825975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:00:40+00:00 2026-06-14T07:00:40+00:00

Here is the problem. Each item has an index value, and the slots it

  • 0

Here is the problem. Each item has an index value, and the slots it could fit into.

items = ( #(index, [list of possible slots])
    (1, ['U', '3']),
    (2, ['U', 'L', 'O']),
    (3, ['U', '1', 'C']),
    (4, ['U', '3', 'C', '1']),
    (5, ['U', '3', 'C']),
    (6, ['U', '1', 'L']),
)

What is the largest list of slots with these items fit into. No slot can be you more than once.

My solution seems hard to follow, and very non-pythonic [and fails on the last item]. I didn’t want to ask a “what’s better” question before solving the prob myself [so now hear I am, beggar’s hat in hand]. Here’s my code:

def find_available_spot(item, spot_list):
    spots_taken = [spot for (i,spot) in spot_list]
    i, l = item
    for spot in l:
        if spot not in spots_taken: return (i, spot)
    return None

def make_room(item, spot_list, items, tried=[]):
    ORDER = ['U','C','M','O','1','3','2','L']
    i, l = item
    p_list = sorted(l, key=ORDER.index)
    spots_taken = [spot for (i, spot) in spot_list]

    for p in p_list:
        tried.append(p)
        spot_found = find_available_spot((i,[p]),spot_list)
        if spot_found: return spot_found
        else:
            spot_item = items[spots_taken.index(p)]
            i, l = spot_item
            for s in tried:
                if s in l: l.remove(s)
            if len(l) == 0: return None

            spot_found = find_available_spot((i,l),spot_list)
            if spot_found: return spot_found

            spot_found = make_room((i,l), spot_list, items, tried)
            if spot_found: return spot_found
            return None

items = ( #(index, [list of possible slots])
    (1, ['U', '3']),
    (2, ['U', 'L', 'O']),
    (3, ['U', '1', 'C']),
    (4, ['U', '3', 'C', '1']),
    (5, ['U', '3', 'C']),
    (6, ['U', '1', 'L']),
)

spot_list = []
spots_taken = []
for item in items:
    spot_found = find_available_spot(item, spot_list)
    if spot_found:
        spot_list.append(spot_found)
    else:
        spot_found = make_room(item,spot_list,items)
        if spot_found: spot_list.append(spot_found)
  • 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-06-14T07:00:42+00:00Added an answer on June 14, 2026 at 7:00 am

    Simply trying every possibility has a certain brutal elegance:

    >>> items = (
    ...     (1, ['U', '3']),
    ...     (2, ['U', 'L', 'O']),
    ...     (3, ['U', '1', 'C']),
    ...     (4, ['U', '3', 'C', '1']),
    ...     (5, ['U', '3', 'C']),
    ...     (6, ['U', '1', 'L']),
    ... )
    >>> import itertools
    >>> locs = zip(*items)[1]
    >>> max((len(p), p) for p in itertools.product(*locs) if len(p) == len(set(p)))
    (6, ('U', 'O', 'C', '1', '3', 'L'))
    

    Admittedly it doesn’t scale very well, though.

    [edit]

    .. and, as noted in the comments, it only finds a solution if there’s a filling solution. A slightly more efficient (but still brute-force) solution works even if there isn’t:

    def find_biggest(items):
        for w in reversed(range(len(items)+1)):
            for c in itertools.combinations(items, w):
                indices, slots = zip(*c)
                for p in itertools.product(*slots):
                    if len(set(p)) == len(p):
                        return dict(zip(indices, p))
    
    >>> items = ( (1, ['U', '3']), (2, ['U', 'L', 'O']), (3, ['U', '1', 'C']), (4, ['U', '3', 'C', '1']), (5, ['U', '3', 'C']), (6, ['U', '1']), (7, ['U', '1', 'L']), )
    >>> find_biggest(items)
    {1: 'U', 2: 'O', 3: '1', 4: '3', 5: 'C', 7: 'L'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: My page displays a set of items. Each item has
Here's the problem: A user has a shopping basket containing items, where each item
I have a GridView that displays items, each item is a ListView. the problem
I've got a set of items. Each item has two images and some text.
One user control is a list box where each item in the list has
I have a listing of items on a site. Each item has several categories
I'd like to make a (non-numbered or bulleted) list of items, where each item
I have a datastore thats has ~850 groups and ~19,000 items. Each item may
Ok, here's my problem: I download a list of items from the net and
Here is the problem: I have two columns in a table that, for each

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.