473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get all possible string values

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
7 1820
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
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
Ashish,

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

:-)

Cor
Nov 16 '05 #4
"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
Ashish,

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

So ten times Sorry,

Cor
Nov 16 '05 #6
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
3
by: Gustaf Liljegren | last post by:
I searched for previous answers on this, but couldn't find something fitting. I need advice on how to store decimal numbers with possible null values in memory. The numbers may be negative, so...
0
by: Vizzybit | last post by:
I am attempting to utilise the HBAAPI.dll that can be found at http://hbaapi.sourceforge.net/ (which is also used in hbaverify at http://hbaverify.sourceforge.net/) as I need to get a small subset...
6
by: Georg J. Stach | last post by:
Hello, taken I've declared an enumeration like this: enum Color{ red,green,blue }; The internal representation of Color is of type integer and it's no problem to print out their values. ...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
13
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
0
by: schwehr | last post by:
Hi All, I've got a small logging class that I would like to have a way to optimize away as much as possible with the minimum of macro trickery. I've got working code, but not having done C++...
5
by: patrin | last post by:
Hi All, given the source document: <?xml version="1.0" encoding="UTF-8"?> <root> <child> <test id="1" name="first child"/> </child> <child>
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.