473,785 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

All Possible Combinations of Multiple Lists

I have multiple ArrayLists, could be 2 or 3 or 10 lists, with multiple
values in them. Now what I need to do is to get a combination of all
of them.

For example, if I have 3 lists with the following values:

List 1: 3, 5, 7
List 2: 3, 5, 6
List 3: 2, 9

I would get these combinations

3, 3, 2
3, 3 , 9
3, 5, 2
3, 5, 9
3, 6, 2
3, 6, 9
5, 3, 2
5, 3, 9
5, 5, 2
5, 5, 9
5, 6, 2
5, 6, 9

etc etc....

Now the problem is I cannot do this easily because I do not know how
many lists I have, therefore determine how many loops I need.

Sep 11 '07 #1
3 12776
I forgot to say that I these arraylists are all stored in a list,
therefore I know the count of arraylists I have.

Sep 11 '07 #2
DarkSoul wrote:
I have multiple ArrayLists, could be 2 or 3 or 10 lists, with multiple
values in them. Now what I need to do is to get a combination of all
of them.

For example, if I have 3 lists with the following values:

List 1: 3, 5, 7
List 2: 3, 5, 6
List 3: 2, 9

I would get these combinations

3, 3, 2
3, 3 , 9
3, 5, 2
....
Now the problem is I cannot do this easily because I do not know how
many lists I have, therefore determine how many loops I need.
As you have already found then the solution is recursion.

Below are another coding solution.

Arne

=============== =============== ======

using System;
using System.Collecti ons.Generic;

namespace E
{
public class Permuting<T>
{
public delegate void Processor(T[] a);
private static void Permute(T[] res, int ix, List<List<T>dat a,
Processor p)
{
foreach(T v in data[ix])
{
res[ix] = v;
if(ix >= data.Count - 1)
{
p(res);
}
else
{
Permute(res, ix + 1, data, p);
}
}
}
public static void Permute(List<Li st<T>data, Processor p)
{
Permute(new T[data.Count], 0, data, p);
}
}
public class TestClass
{
public static void Print1(string[] a)
{
for(int i = 0; i < a.Length; i++)
{
Console.Write(a[i]);
}
Console.WriteLi ne();
}
public static void Print2(int[] a)
{
for(int i = 0; i < a.Length; i++)
{
if(i 0)
{
Console.Write(" , ");
}
Console.Write(a[i]);
}
Console.WriteLi ne();
}
public static void Main(string[] args)
{
List<List<strin g>data1 = new List<List<strin g>>();
data1.Add(new List<string>()) ;
data1.Add(new List<string>()) ;
data1.Add(new List<string>()) ;
data1.Add(new List<string>()) ;
data1[0].Add("A");
data1[0].Add("B");
data1[0].Add("C");
data1[0].Add("D");
data1[1].Add("X");
data1[1].Add("Y");
data1[1].Add("Z");
data1[2].Add("1");
data1[2].Add("2");
data1[3].Add("*");
data1[3].Add("#");
Permuting<strin g>.Permute(data 1, Print1);
List<List<int>d ata2 = new List<List<int>> ();
data2.Add(new List<int>());
data2.Add(new List<int>());
data2.Add(new List<int>());
data2[0].Add(3);
data2[0].Add(5);
data2[0].Add(7);
data2[1].Add(3);
data2[1].Add(5);
data2[1].Add(6);
data2[2].Add(2);
data2[2].Add(9);
Permuting<int>. Permute(data2, Print2);
Console.ReadKey ();
}
}
}
Sep 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
9480
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
3
2942
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process. My Current Form (6 tabs): - Owner, Property, Title, Docs, Queries, & Reports - User is able to see (while navigating through the tabs) above in the form : Owner Name, Address, Parcel, and SSN
1
4271
by: M a n i s h | last post by:
i have been trying to build a program to find various combinations of a string. the problem is that if there are multiple similar characters in the string then the program displays multiple similar combinations. how could i overcome this ...comparing each o/p doesn't seem feasible.
7
1839
by: Caroline | last post by:
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
16
3561
by: akameswaran | last post by:
Ok, this is really irritating me. I'm sure there are different ways of doing this - I'm interested in the algo, not the practical solution, I'm more trying to play with iterators and recursion. I want to create a program that generates every possible combination of a set of a n dice, with s sides. so I started with an iterator class die(object): def __init__(self,sides):
8
5637
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible combination(12!) I hacked a few lines of code and tried a few things from Python CookBook (http://aspn.activestate.com/ASPN/Cookbook/), but they are hell slow.
10
19935
by: D | last post by:
Hello everyone - I'm trying to compile an application to generate all possible 3 digit combinations using 0-9 and a-z, I've looked everywhere for a solution and I found Combinations! v 2.0 for windows, but I was more looking to implement my own solution rather than buy software to perform a small task. Thanks!
11
7175
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
9
3029
by: nickvans | last post by:
Hello everyone, I'm trying to create an MS Access 03 database to be used in optimizing flow for various tasks. The idea is I have a table with task names, duration of each task, and the name of a task that needs to be completed before the current task may commence. A typical example would be: AutoNumber / TaskName / Duration / GroupName 1 / Take out trash / 5 minutes / None 2 / Put in fresh bag / 1 minute / Group 2 3 / Watch TV / 30...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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
10095
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
9954
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
6741
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.