C
C#7d ago
Zirk

✅ Finding all possibilities of N length in an array

Hey so I have the Following code which basically iterate on all my tiles and create arrays of 3 of them, iterating on all possibilities
List<Tile[]> tiles = [];
for (int i = 0; i < matching.Length - 1; i++)
{
for (int y = i + 1; y < matching.Length - 1; y++)
{
tiles.Add([tile, matching[i], matching[y]]);
}
}
return tiles;
List<Tile[]> tiles = [];
for (int i = 0; i < matching.Length - 1; i++)
{
for (int y = i + 1; y < matching.Length - 1; y++)
{
tiles.Add([tile, matching[i], matching[y]]);
}
}
return tiles;
I'm trying to make this code generic so instead of arrays of 3, it can be array of N, but I'm not sure how to approche that Could someone please help me with that?
12 Replies
ero
ero7d ago
What is matching in this case? You're checking its length, but then access Deck. Seems like a mistake?
Zirk
ZirkOP7d ago
Hmm, yeah it's some old code and I think it was indeed a mistake I'll edit the post but it's supposed to be the same list yeah my bad
ero
ero7d ago
if ((matching.Length % N) != 0)
throw new ArgumentException();

for (int i = 0; i < matching.Length - N; i += N)
{
var arr = new Tile[N + 1];
arr[0] = tile;

for (int j = 0; j < N; j++)
{
arr[1 + j] = matching[i + j];
}

tiles.Add(arr);
}
if ((matching.Length % N) != 0)
throw new ArgumentException();

for (int i = 0; i < matching.Length - N; i += N)
{
var arr = new Tile[N + 1];
arr[0] = tile;

for (int j = 0; j < N; j++)
{
arr[1 + j] = matching[i + j];
}

tiles.Add(arr);
}
Something like that
Zirk
ZirkOP7d ago
I'll need to put that on a paper so my two braincells can understand what is happening but I'll investigate that, thanks a lot
ero
ero7d ago
There's an easier way actually
Zirk
ZirkOP7d ago
Oh, what is it if I may ask?
ero
ero7d ago
if ((matching.Length % N) != 0)
throw new ArgumentException();

for (int i = 0; i < matching.Length - N; i += N)
{
var arr = new Tile[N + 1];
arr[0] = tile;
matching.AsSpan(i).CopyTo(arr, 1);

tiles.Add(arr);
}
if ((matching.Length % N) != 0)
throw new ArgumentException();

for (int i = 0; i < matching.Length - N; i += N)
{
var arr = new Tile[N + 1];
arr[0] = tile;
matching.AsSpan(i).CopyTo(arr, 1);

tiles.Add(arr);
}
This should probably also work? Or if you don't like the tospan Mh nah the alternative would suck Ah wait you need to slice matching as well
Zirk
ZirkOP7d ago
So from what I understand is final size is 3 and I have [1, 2, 3, 4, 5], 0 given It would give 0, 1, 2, 3 0, 2, 3, 4 0, 3, 4, 5 Right? wait no let me edit
ero
ero7d ago
Ah, was that the goal? I assumed 1, 2, 3, 4, 5, 6 etc
Zirk
ZirkOP7d ago
Maybe im wrong im just trying to understand your code
0 (given)
1 2 3

0 1 2
0 1 3
0 2 3
0 (given)
1 2 3

0 1 2
0 1 3
0 2 3
my goal was this To get all different possibilities So with
0
1 2 3 4

0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
0
1 2 3 4

0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
But with 4 values instead of 3 it increase a lot the amount of combinaisons
ero
ero7d ago
for (int i = 0; i < matching.Length - N; i++)
{
var arr = new Tile[N + 1];
arr[0] = tile;

for (int j = 0; j < N; j++)
{
arr[1 + j] = matching[i + 1 + j];
}

tiles.Add(arr);
}
for (int i = 0; i < matching.Length - N; i++)
{
var arr = new Tile[N + 1];
arr[0] = tile;

for (int j = 0; j < N; j++)
{
arr[1 + j] = matching[i + 1 + j];
}

tiles.Add(arr);
}
Shouldn't be more complicated than this then
Zirk
ZirkOP7d ago
Oh that's simpler than expected I'll try with that, thanks :)

Did you find this page helpful?