Dunce Cap Firmly On Head

  • Thread starter Thread starter Frank Oquendo
  • Start date Start date
F

Frank Oquendo

This should be easy but I'm drawing a blank. I have N arrays of strings.
Each array has X elements. I need to output every possible combination
of these strings as follows:

1) Array1 = {"A"}
2) Array2 = {"B", "C"}
3) Array3 = {"D", "E", "F"}

The output should be:

A
A.B
A.B.D
A.B.E
A.B.F

Since I won' know how many arrays will be passed in until runtime, I'm
thinking a recursive function is the way to go but I've yet to produce
anything useful. Any ideas?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Frank Oquendo said:
This should be easy but I'm drawing a blank. I have N arrays of strings.
Each array has X elements. I need to output every possible combination
of these strings as follows:

1) Array1 = {"A"}
2) Array2 = {"B", "C"}
3) Array3 = {"D", "E", "F"}

The output should be:

A
A.B
A.B.D
A.B.E
A.B.F

Since I won' know how many arrays will be passed in until runtime, I'm
thinking a recursive function is the way to go but I've yet to produce
anything useful. Any ideas?

Hi Frank,

Here's a discussion that may help:

http://www.csharp-station.com/ShowPost.aspx?PostID=2996

I smile every time I see this: :)

Joe
 
Heh. A puzzle!

Do you want B.A, and F.C.A?
Should B.C be displayed?
Or is it a directed graph kind of thing?
Is the display or print order significant?
 
Mickey said:
Heh. A puzzle!

Do you want B.A, and F.C.A?
Should B.C be displayed?

Nope. All strings should start with "A".
Or is it a directed graph kind of thing?

The first array (a single string, really) is the root for all output
strings. Each other array represents an additional level of branching.
Is the display or print order significant?

Not especially but the algorithm should group like strings together so
that A.B.D and A.B.E are continguous.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Frank said:
The output should be:

A
A.B
A.B.D
A.B.E
A.B.F

Oops! That should be like this:

A
A.B
A.C
A.B.D
A.B.E
A.B.F
A.C.D
A.C.E
A.C.F

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Hi Frank,

Try this:

static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
ArrayList list = new ArrayList();
string[] Array1 = {"A"};
string[] Array2 = {"B", "C"};
string[] Array3 = {"D", "E", "F"};
list.Add( Array1);
list.Add( Array2);
list.Add( Array3);
Combine( list, 0, 0, "");
Console.ReadLine();
}

static void Combine( ArrayList lists, int indexOFarray, int indexINarray,
string current)
{
//write the current value
int index = indexINarray;

while( index < ((string[])lists[indexOFarray]).Length )
{
Console.WriteLine( current + ((string[])lists[indexOFarray])[index] );
//if not the last array index go deep
if ( lists.Count > indexOFarray +1 )
//There is other array after this, go deep
Combine( lists, indexOFarray+1, 0, current +
((string[])lists[indexOFarray])[index] );
index++;
}

}


Hope this help,
 
Frank said:
<off to study the code>

Okay... what if I have an array of strings that is to be added to the
end of every level?

Array1 = {"Level1", "Level2", "Level3"}
Array 2 = {"A", "B", "C"}

Output:
Level1
Level1.A
Level1.B
Level1.C
Level1.Level2
Level1.Level2.A
Level1.Level2.B
Level1.Level2.C
Level1.Level2.Level3
Level1.Level2.Level3.A
Level1.Level2.Level3.B
Level1.Level2.Level3.C

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
static void Main(string[] args)
{
string [] Array1 = {"Level1", "Level2", "Level3"};
string [] Array2 = {"A", "B", "C"};
for(int i=0;i<Array1.Length;++i)
{
foreach(string str in Array2)
{
for(int j=0;j<=i;++j)
{
Console.Write(Array1[j] + ".");
}
Console.WriteLine(str);
}
}
Console.ReadLine();
}

- Pete
 
Back
Top