Number of combinations

  • Thread starter Thread starter csfong33
  • Start date Start date
C

csfong33

Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!
 
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Hi,

Maybe:

string s[] = new string[arr1.Length*arr2.Length*arr3.Length];

for( int i=0; i<arr1.Length; ++i )
for( int j=0; j<arr1.Length; ++j )
for( int k=0; k<arr1.Length; ++k )
s[i+j+k] = String.Concat(s, s[j], s[k]);

Hope this helps.
Moty
 
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Three nested "for" loops shoud give the result that you want:

string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
int i=0;
foreach (string s1 in arr1)
foreach (strng s2 in arr2)
foreach (string s3 in arr3)
arr_result[i++]=s1+s2+s3;
 
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Hi,

Maybe:

string s[] = new string[arr1.Length*arr2.Length*arr3.Length];

for( int i=0; i<arr1.Length; ++i )
for( int j=0; j<arr1.Length; ++j )
for( int k=0; k<arr1.Length; ++k )
s[i+j+k] = String.Concat(s, s[j], s[k]);

Hope this helps.
Moty


Your code maybe wrong, especially the index of s.
I think it should be this:

s[i * arr2.Length * arr3.Length + j * arr3.Length + k] =
String.Concat(arr1, arr2[j], arr3[k]);
 
In said:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];

Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.
 
In said:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];

Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.

Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...
 
....
Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...

Without syntax highlighting or checking the actual solution, I wrote
the following:

/************************************************/
public static string[] FlattenArrays(System.Array[] arrays)
{
if (arrays.Length < 1) throw new System.Exception();

else if (arrays.Length == 1) return arrays[0];

else
{
string[] recursed =
FlattenArrays( SomeMethodForSelectingSublist(arrays, 0, arrays.Length
- 2) );
int currentArrayIndex = arrays.Length-1;

for (int i = 0; i < arrays[currentArrayIndex].Length; i++)
recursed = recursed + arrays[currentArrayIndex];

return recursed;
}
}
/************************************************/

Perhaps if you improve this piece of code you'll get where you want to.
 
You want the Cartesian product of the three arrays. For a solution that
does not depend on nested loops (works for any number of arrays), see
http://www.frontiernet.net/~fredm/dps/chapter02.doc . The code works with
Lists, but you could convert it to work with arrays (or convert your arrays
to Lists).

The code to take the Cartesian product will take any list of lists and
return each combination via a "foreach" iterator. So, your calling code
would be something like:

List<List<string>> myArrays; //convert your arrays to lists, put
them here

Cartesian<List<string>>answer =
new Cartesian<List<string>>(myArrays);

foreach (List<string> vector in answer.cartesian())
{
//do something with the vector

}


The code to make the CP is included in the library mentioned in the book.
It is:

namespace Search
{

public class Cartesian<T>
{
List<List<T>> toCross;
int[] vector;
public int totalElements = 1;

public Cartesian(List<List<T>> list)
{

toCross = list;

vector = new int[toCross.Count];

foreach (List<T> aList in list)
totalElements *= aList.Count;
}

public IEnumerable<List<T>> cartesian()
{
reset();
while (true)
{
yield return makeList();
int startAt = toCross.Count - 1;
while (vector[startAt] == toCross[startAt].Count - 1)
{
vector[startAt] = 0;
startAt--;
if (startAt < 0)
break;
}
if (startAt < 0)
break;

vector[startAt]++;
}
}

List<T> makeList()
{
List<T> ans = new List<T>
(toCross.Count);

for (int k = 0; k < vector.Length; k++)
{
ans.Add((toCross[k])[vector[k]]);
}
return ans;
}

public void reset()
{
for (int i = 0; i < toCross.Count; i++)
{
vector = 0;
}
}
}
}
 
In said:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.

Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...

I made the code below for a similar problem, maybe it could
inspire you.

Arne

========================================

using System;
using System.Collections.Generic;

namespace E
{
public class Permuting
{
public delegate void Processor(string s);
public static void Print(string s)
{
Console.WriteLine(s);
}
private static void Permute(string prefix, int ix, List<List<String>>
data, Processor p)
{
foreach(string s in data[ix])
{
if(ix >= data.Count - 1)
{
p(prefix + s);
}
else
{
Permute(prefix + s, ix + 1, data, p);
}
}
}
public static void Permute(List<List<String>> data, Processor p)
{
Permute("", 0, data, p);
}
public static void Main(string[] args)
{
List<List<String>> data = new List<List<String>>();
data.Add(new List<String>());
data.Add(new List<String>());
data.Add(new List<String>());
data[0].Add("A");
data[0].Add("B");
data[0].Add("C");
data[0].Add("D");
data[1].Add("X");
data[1].Add("Y");
data[1].Add("Z");
data[2].Add("1");
data[2].Add("2");
Permute(data, Print);
Console.ReadLine();
}
}
}
 
In a previous message I wrote:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.
Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...

I made the code below for a similar problem, maybe it could
inspire you.

Arne

========================================

using System;
using System.Collections.Generic;

namespace E
{
public class Permuting
{
public delegate void Processor(string s);
public static void Print(string s)
{
Console.WriteLine(s);
}
private static void Permute(string prefix, int ix, List<List<String>>
data, Processor p)
{
foreach(string s in data[ix])
{
if(ix >= data.Count - 1)
{
p(prefix + s);
}
else
{
Permute(prefix + s, ix + 1, data,p);
}
}
}
public static void Permute(List<List<String>> data, Processor p)
{
Permute("", 0, data, p);
}
public static void Main(string[] args)
{
List<List<String>> data = new List<List<String>>();
data.Add(new List<String>());
data.Add(new List<String>());
data.Add(new List<String>());
data[0].Add("A");
data[0].Add("B");
data[0].Add("C");
data[0].Add("D");
data[1].Add("X");
data[1].Add("Y");
data[1].Add("Z");
data[2].Add("1");
data[2].Add("2");
Permute(data, Print);
Console.ReadLine();
}
}



}- Hide quoted text -

- Show quoted text -


Thanks for all the help!
 
Back
Top