423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

get all possible string values

P: n/a
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?
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I should add that the lists can be of different lengths to each other...

Caroline

"Caroline" <ca***********@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
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?

Nov 16 '05 #2

P: n/a
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" <ca***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I should add that the lists can be of different lengths to each other...

Caroline

"Caroline" <ca***********@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
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?


Nov 16 '05 #3

P: n/a
Ashish,

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

:-)

Cor
Nov 16 '05 #4

P: n/a
"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl... Ashish,

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

:-)

Cor

Nov 16 '05 #5

P: n/a
Ashish,

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

So ten times Sorry,

Cor
Nov 16 '05 #6

P: n/a
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" <yh****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <ca***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I should add that the lists can be of different lengths to each other...

Caroline

"Caroline" <ca***********@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
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?



Nov 16 '05 #7

P: n/a
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" <ca***********@gmail.com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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" <yh****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <ca***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I should add that the lists can be of different lengths to each other...

Caroline

"Caroline" <ca***********@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
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?



Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.