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.
>>
– Elia Giaccardi Mar 30 '22 at 08:02