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 7756315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:47:28+00:00 2026-06-01T12:47:28+00:00

Imagine a 3×3 grid: [A, B, %] [C, %, D] [E, F, G] The

  • 0

Imagine a 3×3 grid:

[A, B, %]
[C, %, D]
[E, F, G]

The percentages % stand for empty spaces/positions.

The rows can be moved like beads on a string, such that the permutations for the configurations for the first row could be any one of:

[A, B, %] or [A, %, B] or [%, A, B]

Similarly for the second row. The third row contains no empty slots and so cannot change.

I am trying to produce all possible grids, given the possible permutations of each row.

The output should produce the following grids:

[A, B, %]    [A, B, %]    [A, B, %]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

[A, %, B]    [A, %, B]    [A, %, B]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

[%, A, B]    [%, A, B]    [%, A, B]
[C, D, %]    [C, %, D]    [%, C, D]
[E, F, G]    [E, F, G]    [E, F, G]

I have tried a method of looking through each row and shifting the space left and right, then generating new grids off that and recursing. I keep all grids in a set and ensure I only produce positions which haven’t already been examined to prevent infinite recursion.

However, my algorithm seems to be horrendously inefficient (~1s per permutation!!) and doesn’t look very nice either. I was wondering if there was an eloquent way of doing this? In python in particular.

I have some vague ideas but I’m sure there is a way of doing this which is short and simple which I’m overlooking.

EDIT: 3×3 is just an example. Grid could be of any size and it’s really the row combinations which matter. For example:

[A, %, C]
[D, E, %, G]
[H, I]

is also a valid grid.

EDIT 2: The letters must maintain their order. For example [A, %, B] != [B, %, A] and [B, A, %] isn’t valid

  • 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-01T12:47:29+00:00Added an answer on June 1, 2026 at 12:47 pm

    First you have to get all desired permutations for each line. Then you calculate the cross product of all lines.

    The permutations of a line can be simple calculated by having the letters [A,B,%] and varying the starting index:

    import itertools
    # Example: line = ['A','B','%']
    def line_permutations(line):
       if '%' not in line:
           return [line]
       line.remove('%') # use copy.copy if you don't want to modify your matrix here
       return (line[:i] + ['%'] + line[i:] for i in range(len(line) + 1))
    

    The cross product is easiest to achieve using itertools.product

    matrix = [['A','B','%'], ['C', '%', 'D'], ['E', 'F', 'G']]
    permutations = itertools.product(*[line_permutations(line) for line in matrix])
    for p in permutations:
        print(p)
    

    This solution is optimal in memory and CPU requirements, because permutations are never recomputed.

    Example output:

    (['%', 'A', 'B'], ['%', 'C', 'D'], ['E', 'F', 'G'])
    (['%', 'A', 'B'], ['C', '%', 'D'], ['E', 'F', 'G'])
    (['%', 'A', 'B'], ['C', 'D', '%'], ['E', 'F', 'G'])
    (['A', '%', 'B'], ['%', 'C', 'D'], ['E', 'F', 'G'])
    (['A', '%', 'B'], ['C', '%', 'D'], ['E', 'F', 'G'])
    (['A', '%', 'B'], ['C', 'D', '%'], ['E', 'F', 'G'])
    (['A', 'B', '%'], ['%', 'C', 'D'], ['E', 'F', 'G'])
    (['A', 'B', '%'], ['C', '%', 'D'], ['E', 'F', 'G'])
    (['A', 'B', '%'], ['C', 'D', '%'], ['E', 'F', 'G'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a Model/Collection like: var AModel = Backbone.Model.extend({ defaults: { a: 'a string', b:
imagine if you will the following string: A Great Movie (1999) DVDRip.avi I am
Imagine a robot sitting on the upper left hand corner of an NxN grid.
Imagine the following scenario. I have a checkbox that the user can check to
Imagine this is my URL NSURL *theURL=[NSURL URLWithString:@http://stackoverflow.com/questions/ask]; How can i extract only: http://stackoverflow.com
imagine a database setup, where x customers can make y orders. normally all customers
Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine an HTML form and the jQuery library is loaded. I'd like it such
Imagine I have one common functionality: series and number (string and integer) of some
Imagine a scenario you have a text file with say 10000 rows in it

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.