473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1839
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.goo gle.com...
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******** ********@TK2MSF TNGP10.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.goo gle.com...
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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP10.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.goo gle.com...
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<ListCollectio n.Count; x++ )
{
string accCode = "";
BuildAccountCod e( x, ListCollection. Count, accCode);
}

private void BuildAccountCod e(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 )
{
BuildAccountCod e(listNum+1, count, accountCode);
}
else
{
SaveAccountCode (accountCode);
}
}
}

"Caroline" <ca***********@ gmail.com> wrote in message
news:OG******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP10.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.goo gle.com...
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
2256
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 DataTable first and I then want to roll through the DataTable while in memory checking for errors and then commit the rows to my database table (btw this is in ASP.NET). Is it possible to have data in a datable before attaching at DataAdapter? I'm a...
40
2622
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 indexing) symmetrical and more consistent. So to find out if this is indeed a possibility, it would be nice to get a few opinions at this point. So blast away... or hopefully tell me what you like about it instead. ;-) (Any suggestions or...
3
14387
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 storing null values as -1 doesn't work. The numbers are amounts of money, so decimal is the best datatype, except that it can't store null values. The best idea so far is to make an object of each number and have it store either a decimal or null....
0
2203
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 of the data that it provides. However, although I can get the initialisations to work, I cannot manage to get responses that require data to pass to the dll. (The latest API details are at : ftp://ftp.t11.org/t11/pub/sm/hba/05-056v0.pdf) The...
6
1792
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. But is it possible to get their string representing value _without_ having a
5
1999
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 called "students". I need to update a field called "status" on this table for all members that have never attended a class. Class attendance is recorded by another table (which represents the many to
13
2553
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 report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
0
1123
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++ much for a few years, I have the feeling that this code might not be the best. I'd like to be able to have different source files compile in different states so that the high performance cores can optionally be built with logging dissapearing as...
5
1804
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
1725
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 (like a punchcard). The columns contain integer, real, and date values. The output files are the same values in binary. I have to parse the values and write the binary tuples out into the correct file based on a given column. It's a little more...
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.