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!
Moty Michaely - 14 Jun 2007 10:32 GMT
On Jun 14, 12:16 pm, csfon...@gmail.com wrote:
> Hi,
> I have arrays below:
[quoted text clipped - 14 lines]
>
> 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[i], s[j], s[k]);
Hope this helps.
Moty
icemaple - 14 Jun 2007 10:52 GMT
At Thu, 14 Jun 2007 17:32:50 +0800,Moty Michaely <Moty.Mi@gmail.com>
wrote:
> On Jun 14, 12:16 pm, csfon...@gmail.com wrote:
>> Hi,
[quoted text clipped - 29 lines]
> 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[i], arr2[j], arr3[k]);

Signature
Best Regards.
Maple
Alberto Poblacion - 14 Jun 2007 10:52 GMT
> Hi,
> I have arrays below:
[quoted text clipped - 12 lines]
>
> 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;
Alberto Poblacion - 14 Jun 2007 11:02 GMT
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.
csfong33@gmail.com - 14 Jun 2007 11:43 GMT
On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> 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...
Jeroen - 14 Jun 2007 11:54 GMT
> ....
> 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[i] = recursed[i] + arrays[currentArrayIndex][i];
return recursed;
}
}
/************************************************/
Perhaps if you improve this piece of code you'll get where you want to.
Arne Vajhøj - 15 Jun 2007 01:54 GMT
> On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
> quitaestoparacontes...@poblacion.org> wrote:
[quoted text clipped - 4 lines]
> 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();
}
}
}
csfong33@gmail.com - 15 Jun 2007 02:12 GMT
On Jun 15, 8:54 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> csfon...@gmail.com wrote:
> > On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
[quoted text clipped - 67 lines]
>
> - Show quoted text -
Thanks for all the help!
Fred Mellender - 14 Jun 2007 12:51 GMT
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[i] = 0;
}
}
}
}
> Hi,
> I have arrays below:
[quoted text clipped - 14 lines]
>
> Thanks!
Ignacio Machin ( .NET/ C# MVP ) - 14 Jun 2007 15:25 GMT
This sounds like homework :)
> Hi,
> I have arrays below:
[quoted text clipped - 14 lines]
>
> Thanks!