1

I have a weird queston: I'd like to get a length as input and get lists of idexes of every possible combination of the values that would be in a list of the specified length.

For example, with a length of 4 I would get:

{1, 2} with {3, 4};

{1, 4} with {2, 3};

{1, 3} with {2, 4};

{1, 2, 3} with {4};

{1, 2, 4} with {3};

{2, 3, 4} with {1};

{1, 3, 4} with {2};

{1, 2, 3, 4};

(in no particular order)

Or with a length of 6 I'd get:

{1, 2} with {3,4} with {5, 6};

etc.

{1, 2, 3} with {4, 5, 6};

etc.

{1, 2, 3} with {4, 5} with {6};

etc.

{1, 2, 3, 4} with {5, 6};

etc.

{1, 2, 3, 4, 5} with {6};

etc.

{1, 2, 3, 4, 5, 6};

I'm not sure if I've included every combination in the examples but I hope the idea is clear.

I code in c#, if anybody wants to share an answer.

This is what I got so far:

List<List<List<List<int>>>> PossibleIndexesCombinations(int length)
{
    List<List<List<List<int>>>> possibleIndexesCombinations = new List<List<List<List<int>>>>();

    for (int i = 0; i < length; i++) // instantiate the first layer of lists
    {
        possibleIndexesCombinations.Add(new List<List<List<int>>>());
    }

    for (int i = 0; i < length; i++) // instantiate the second layer of lists
    {
        for (int a = 0; a < length; a++)
        {
            possibleIndexesCombinations[a].Add(new List<List<int>>());
        }
    }

    for (int i = 0; i < length; i++) // instantiate the third layer of lists
    {
        for (int a = 0; a < length; a++)
        {
            for (int n = 0; n < length; n++)
            {
                possibleIndexesCombinations[a][n].Add(new List<int>(2));
            }
        }
    }

    for (int i = 0; i < length; i++) // iterate through every value
    {
        string consoleString = "";

        for (int amm = 1; amm < length; amm++) // iterate through the different amounts of values => {1} = 1, {1, 2} = 2, {1, 2, 3} = 3
        {
            for (int iteration = 0; iteration < amm; iteration++) // set all of the values
            {
                int value = i + iteration;

                if (value >= length) value -= length;

                possibleIndexesCombinations[i][amm - 1][0].Add(value);

                consoleString += value + " ";
            }

            consoleString += "; ";

            for (int value = 0; value < length; value++) // set the ramaing values to whatever values haven't yet been used
            {
                bool newValue = true;

                foreach (int checkingValue in possibleIndexesCombinations[i][amm - 1][0])
                {
                    if (checkingValue == value) newValue = false;
                }

                if (newValue)
                {
                    possibleIndexesCombinations[i][amm - 1][1].Add(value);

                    consoleString += value + " ";
                }
            }

            consoleString += ". ";
        }

        Debug.Log(consoleString);
    }

    return possibleIndexesCombinations;
}

With a length of 4 the 4 outputs, as expected, are:

0 ; 1 2 3 . 0 1 ; 2 3 . 0 1 2 ; 3 .

1 ; 0 2 3 . 1 2 ; 0 3 . 1 2 3 ; 0 .

2 ; 0 1 3 . 2 3 ; 0 1 . 2 3 0 ; 1 .

3 ; 0 1 2 . 3 0 ; 1 2 . 3 0 1 ; 2 .

The only thing missing here is that this method will always exclusively return two lists of numbers, while I would like to sometimes get more (as shown in the examples above, with a length of 6 I'd like to also get things like {1, 2} with {3,4} with {5, 6} etc, or with a length of 8 things like {1, 2, 3} with {4, 5, 6} with {7, 8} or like {1, 2, 3, 4} with {5, 6, 7} with {8})

If any of you have any thoughts, please feel free to share them.

  • So single item lists are invalid? Do you have any code to share of your efforts? – ChiefTwoPencils Mar 30 '22 at 07:45
  • 1
    I think it would rather make the desired outcome _clearer_. Perhaps you could edit your question to include _all_ the resulting list combinations you would expect to get for the length 3 or 4? Also, as ChiefTwoPencils asks, please show your efforts. It doesn't have to be code, it can be pseudo code / your thoughts. – Astrid E. Mar 30 '22 at 08:01
  • I didn't put single item lists in beacuse I thought it would make it more confusing then it needs to be, but it wouldn't change anaything in the program. I haven't yet figured out a way to start writing this, the only thing I got is that it would return a List>> – Elia Giaccardi Mar 30 '22 at 08:02
  • 1
    Well it would change it because you'd be actively disregarding the permutations that include all the single element arrays. I think you could start it. {1} with {rest}, {1,2} with {rest}....{2} with {rest} and so on. Alter for your requirements; the more info the merrier. – ChiefTwoPencils Mar 30 '22 at 08:03

0 Answers0