get all possible string values

  • Thread starter Thread starter Caroline
  • Start date Start date
C

Caroline

I need to write a method that accepts say three lists of strings and
generates all possible combinations of strings from these lists in the
order that the lists are provided.

eg:

list1 list2 list3
A B Z
B C Y
C D X

The method would need to generate the following strings:

ABZ
ABY
ABX
ACZ
ACY
ACX
ADZ
ADY
ADZ
BBZ.... and so on up to CDX

Can anyone help me?
 
Code is given below, you can extend it and make it more dynamic for any
number of lists and number or items in any list

string [] List1 = {"A","B","C"};
string [] List2 = {"B","C","D"};
string [] List3 = {"Z","Y","X"};

foreach(string s1 in List1)
{
foreach(string s2 in List2)
{
foreach(string s3 in List3)
{
string s = s1+s2+s3;
}
}
}
 
Ashish,

I think that your solution give doubles.
However it is a nice student problem.

:-)

Cor
 
Cor Ligthert said:
Ashish,

I think that your solution give doubles.
However it is a nice student problem.

:-)

Cor

Cor,

I am not sure where you see double as that code can not give double values.
Output with this kind of combenation is 3^3 = 27. A sample output for you in
case you did not run that code. I agree that it is a simple programming 101
problem.

ABZ
ABY
ABX
ACZ
ACY
ACX
ADZ
ADY
ADX
BBZ
BBY
BBX
BCZ
BCY
BCX
BDZ
BDY
BDX
CBZ
CBY
CBX
CCZ
CCY
CCX
CDZ
CDY
CDX

and now with some numbers

147
148
149
157
158
159
167
168
169
247
248
249
257
258
259
267
268
269
347
348
349
357
358
359
367
368
369
 
Ashish,

Sorry that I had doubts, I did it on first sight,

So ten times Sorry,

Cor
 
Thanks for that, however the problem I have and should have mentioned at the
start, is that I don't know how many lists I am going to have to work with.
Therefore I can't write nested loops to cater for each list.

I have a feeling that a recursive function is required but I'm not quite
sure how to write it.

Caroline

Ashish Das said:
Code is given below, you can extend it and make it more dynamic for any
number of lists and number or items in any list

string [] List1 = {"A","B","C"};
string [] List2 = {"B","C","D"};
string [] List3 = {"Z","Y","X"};

foreach(string s1 in List1)
{
foreach(string s2 in List2)
{
foreach(string s3 in List3)
{
string s = s1+s2+s3;
}
}
}






Caroline said:
I should add that the lists can be of different lengths to each other...

Caroline
 
I finally figured it out.

This is how i did it:

I have a collection of lists called ListCollection:

....
foreach ( int x=0; x<ListCollection.Count; x++ )
{
string accCode = "";
BuildAccountCode( x, ListCollection.Count, accCode);
}

private void BuildAccountCode(int listNum, int count, string accCode)
{
string accountCode = "";

for ( int x=0; x < ListCollection[listNum].Count; x++)
{
accountCode = accCode + ListCollection[listNum][x].ToString();
if ( listNum+1 < count )
{
BuildAccountCode(listNum+1, count, accountCode);
}
else
{
SaveAccountCode(accountCode);
}
}
}

Caroline said:
Thanks for that, however the problem I have and should have mentioned at
the start, is that I don't know how many lists I am going to have to work
with. Therefore I can't write nested loops to cater for each list.

I have a feeling that a recursive function is required but I'm not quite
sure how to write it.

Caroline

Ashish Das said:
Code is given below, you can extend it and make it more dynamic for any
number of lists and number or items in any list

string [] List1 = {"A","B","C"};
string [] List2 = {"B","C","D"};
string [] List3 = {"Z","Y","X"};

foreach(string s1 in List1)
{
foreach(string s2 in List2)
{
foreach(string s3 in List3)
{
string s = s1+s2+s3;
}
}
}






Caroline said:
I should add that the lists can be of different lengths to each other...

Caroline

I need to write a method that accepts say three lists of strings and
generates all possible combinations of strings from these lists in the
order that the lists are provided.

eg:

list1 list2 list3
A B Z
B C Y
C D X

The method would need to generate the following strings:

ABZ
ABY
ABX
ACZ
ACY
ACX
ADZ
ADY
ADZ
BBZ.... and so on up to CDX

Can anyone help me?
 
Back
Top