
July 16th, 2003, 10:45 AM
|
|
Contributing User
|
|
Join Date: Mar 2001
Location: Dublin
Posts: 413
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
|
|
this (c#) function does what you're looking for (hopefully)
Code:
public static String[] getCombinations(String[] sourceArray,int size)
{int i,j;
int sourceLength=sourceArray.Length;
if (sourceLength<size) {size=sourceLength;}
int[] indices=new int[size];
int count=1;
for (i=0;i<size;)
{count*=(sourceLength-i);
count/=++i;
}
String[] items=new String[count];
int indexCap=1+sourceLength-size;
for (i=0;i<count;++i)
{for (j=0;j<size;++j) {items[i]+=sourceArray[j+indices[j]]+" ";}
items[i]=items[i].Substring(0,items[i].Length-1);
while (0<j)
{indices[--j]++;
if (indices[j]<indexCap) {break;}
}
if (indices[0]<indexCap)
{for (j=0;j<size;++j) {if (indices[j]==indexCap) {indices[j]=indices[j-1];}}}
}
return items;
}
It's set up to return an array of strings - each item being a list of the constituent strings separated by spaces. It's easy enough to change it to accept an array of object references as an input and return a 2 dimensional array of object references instead.
Let me know if you need it explained...
What's the comb() function?
Last edited by epl : July 16th, 2003 at 10:54 AM.
|