473,467 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Number of combinations

Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Jun 14 '07 #1
10 4097
On Jun 14, 12:16 pm, csfon...@gmail.com wrote:
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!
Hi,

Maybe:

string s[] = new string[arr1.Length*arr2.Length*arr3.Length];

for( int i=0; i<arr1.Length; ++i )
for( int j=0; j<arr1.Length; ++j )
for( int k=0; k<arr1.Length; ++k )
s[i+j+k] = String.Concat(s[i], s[j], s[k]);

Hope this helps.
Moty

Jun 14 '07 #2
At Thu, 14 Jun 2007 17:32:50 +0800£¬Moty Michaely <Mo*****@gmail.com>
wrote:
On Jun 14, 12:16 pm, csfon...@gmail.com wrote:
>Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Hi,

Maybe:

string s[] = new string[arr1.Length*arr2.Length*arr3.Length];

for( int i=0; i<arr1.Length; ++i )
for( int j=0; j<arr1.Length; ++j )
for( int k=0; k<arr1.Length; ++k )
s[i+j+k] = String.Concat(s[i], s[j], s[k]);

Hope this helps.
Moty
Your code maybe wrong, especially the index of s.
I think it should be this:

s[i * arr2.Length * arr3.Length + j * arr3.Length + k] =
String.Concat(arr1[i], arr2[j], arr3[k]);

--
Best Regards.
Maple
Jun 14 '07 #3

<cs******@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.
Three nested "for" loops shoud give the result that you want:

string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
int i=0;
foreach (string s1 in arr1)
foreach (strng s2 in arr2)
foreach (string s3 in arr3)
arr_result[i++]=s1+s2+s3;
Jun 14 '07 #4
In a previous message I wrote:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.

Jun 14 '07 #5
On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
In a previous message I wrote:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];

Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.
Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...

Jun 14 '07 #6
....
Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...
Without syntax highlighting or checking the actual solution, I wrote
the following:

/************************************************/
public static string[] FlattenArrays(System.Array[] arrays)
{
if (arrays.Length < 1) throw new System.Exception();

else if (arrays.Length == 1) return arrays[0];

else
{
string[] recursed =
FlattenArrays( SomeMethodForSelectingSublist(arrays, 0, arrays.Length
- 2) );
int currentArrayIndex = arrays.Length-1;

for (int i = 0; i < arrays[currentArrayIndex].Length; i++)
recursed[i] = recursed[i] + arrays[currentArrayIndex][i];

return recursed;
}
}
/************************************************/

Perhaps if you improve this piece of code you'll get where you want to.

Jun 14 '07 #7
You want the Cartesian product of the three arrays. For a solution that
does not depend on nested loops (works for any number of arrays), see
http://www.frontiernet.net/~fredm/dps/chapter02.doc . The code works with
Lists, but you could convert it to work with arrays (or convert your arrays
to Lists).

The code to take the Cartesian product will take any list of lists and
return each combination via a "foreach" iterator. So, your calling code
would be something like:

List<List<string>myArrays; //convert your arrays to lists, put
them here

Cartesian<List<string>>answer =
new Cartesian<List<string>>(myArrays);

foreach (List<stringvector in answer.cartesian())
{
//do something with the vector

}
The code to make the CP is included in the library mentioned in the book.
It is:

namespace Search
{

public class Cartesian<T>
{
List<List<T>toCross;
int[] vector;
public int totalElements = 1;

public Cartesian(List<List<T>list)
{

toCross = list;

vector = new int[toCross.Count];

foreach (List<TaList in list)
totalElements *= aList.Count;
}

public IEnumerable<List<T>cartesian()
{
reset();
while (true)
{
yield return makeList();
int startAt = toCross.Count - 1;
while (vector[startAt] == toCross[startAt].Count - 1)
{
vector[startAt] = 0;
startAt--;
if (startAt < 0)
break;
}
if (startAt < 0)
break;

vector[startAt]++;
}
}

List<TmakeList()
{
List<Tans = new List<T>
(toCross.Count);

for (int k = 0; k < vector.Length; k++)
{
ans.Add((toCross[k])[vector[k]]);
}
return ans;
}

public void reset()
{
for (int i = 0; i < toCross.Count; i++)
{
vector[i] = 0;
}
}
}
}
<cs******@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Jun 14 '07 #8
This sounds like homework :)

<cs******@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
Hi,
I have arrays below:
arr1 = { a, b, c}
arr2 = {1, 2, 3}
arr3 = {x, y, z}

I want to get a combination of these array, as below

arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
"a3z",
"b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
"b3y", "b3z",
"c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
"c3y", "c3z"}

appreciate if someone can help with example algorithm.

Thanks!

Jun 14 '07 #9
cs******@gmail.com wrote:
On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
>In a previous message I wrote:
>>string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.

Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...
I made the code below for a similar problem, maybe it could
inspire you.

Arne

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

using System;
using System.Collections.Generic;

namespace E
{
public class Permuting
{
public delegate void Processor(string s);
public static void Print(string s)
{
Console.WriteLine(s);
}
private static void Permute(string prefix, int ix, List<List<String>>
data, Processor p)
{
foreach(string s in data[ix])
{
if(ix >= data.Count - 1)
{
p(prefix + s);
}
else
{
Permute(prefix + s, ix + 1, data, p);
}
}
}
public static void Permute(List<List<String>data, Processor p)
{
Permute("", 0, data, p);
}
public static void Main(string[] args)
{
List<List<String>data = new List<List<String>>();
data.Add(new List<String>());
data.Add(new List<String>());
data.Add(new List<String>());
data[0].Add("A");
data[0].Add("B");
data[0].Add("C");
data[0].Add("D");
data[1].Add("X");
data[1].Add("Y");
data[1].Add("Z");
data[2].Add("1");
data[2].Add("2");
Permute(data, Print);
Console.ReadLine();
}
}
}
Jun 15 '07 #10
On Jun 15, 8:54 am, Arne Vajhøj <a...@vajhoej.dkwrote:
csfon...@gmail.com wrote:
On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
In a previous message I wrote:
string[] arr_result = new string[arr1.Length+arr2.Length+arr3.Length];
Ooops... Sorry. It should be arr1.Length*arr2.Length*arr3.Length.
Thanks for the reply. I wonder is there any way without for loop ?
I might have 4 or more arrays...

I made the code below for a similar problem, maybe it could
inspire you.

Arne

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

using System;
using System.Collections.Generic;

namespace E
{
public class Permuting
{
public delegate void Processor(string s);
public static void Print(string s)
{
Console.WriteLine(s);
}
private static void Permute(string prefix, int ix, List<List<String>>
data, Processor p)
{
foreach(string s in data[ix])
{
if(ix >= data.Count - 1)
{
p(prefix + s);
}
else
{
Permute(prefix + s, ix + 1, data,p);
}
}
}
public static void Permute(List<List<String>data, Processor p)
{
Permute("", 0, data, p);
}
public static void Main(string[] args)
{
List<List<String>data = new List<List<String>>();
data.Add(new List<String>());
data.Add(new List<String>());
data.Add(new List<String>());
data[0].Add("A");
data[0].Add("B");
data[0].Add("C");
data[0].Add("D");
data[1].Add("X");
data[1].Add("Y");
data[1].Add("Z");
data[2].Add("1");
data[2].Add("2");
Permute(data, Print);
Console.ReadLine();
}
}

}- Hide quoted text -

- Show quoted text -

Thanks for all the help!
Jun 15 '07 #11

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

Similar topics

36
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
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
20
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
2
by: GrantMagic | last post by:
I have found that some strange combinations of characters in a URL can cause an error in my ASP.NET application. This is regarding URL Paramters For example: if i have the URL:...
4
by: Suzie1986 | last post by:
Hiya, I am a newcomer to programming and really stuck!!! Any help would be gratefully received!! I have written a program that gives me all possible combinations for 1 and 2 for a length of...
22
by: MLH | last post by:
If 3 things can be in one of 2 states, the number of possible combinations is equal to 2^3. But if I have 3 things, 2 of which can be in 2 states and the other in 3 states, what's the simplest...
0
by: John Doe77 | last post by:
Hi, Given a phone number I need to print out all the word representation combinations possible from that phone number. Digits translate into chars like the following: 1 = 1 2 = A B C 3 = D E...
5
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all...
1
by: sotirios | last post by:
I have a Table (Table01)in Access with one number field name Num (double) I want a routine to create a new table example Table02 with 2 fields the first with combinations of the numbers of Table01...
6
by: nullgraph | last post by:
Hi everyone, I'm new to Python and the notion of lambda, and I'm trying to write a function that would have a varying number of nested for loops depending on parameter n. This just smells like a...
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
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,...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...

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.