InfoLearner
InfoLearner

Reputation: 15598

Recursively Generate Combination Of Every Item in the list

I have a List

List<string> collection = {"a","b"}

I want to give a numerical value i.e. Weight e.g. 2

What I want is:

For the given weight, get all possible combinations:

a0b0, a1b0, a2b0
a0b1, a1b1, a2b1
a0b2, a2b2

where 0,1,2 are the values from 0 to the given weight value

I am struggle to generate it. Please guide?

Upvotes: 1

Views: 2374

Answers (3)

Bolu
Bolu

Reputation: 8786

Using Recursion:

static void ShowCombination(List<string> mlist, int value,int current=0,string stringleft="")
    {
            if (current == mlist.Count-1) // if this is the last item in the list
            {
                for (int m = 0; m <= value; m++) //loop through the value add it to the existing string-stringleft
                {
                    Console.WriteLine(stringleft  + mlist[current]+m.ToString()); 
                }
            }
            else // if there are more than 1 item left in the list
            {
                string currentstring = mlist[current]; //get current string in the list eg. "a" 
                stringleft = stringleft + currentstring ; //reset existing string -- eg "a"
                for (int m = 0; m <= value; m++)  //loop through the value add it to the existing 'stringleft' pass it and the new current index for recursion
                {
                    string stopass = stringleft +  m.ToString(); // eg. "a0"; "a1" 
                    ShowCombination(mlist, value, current + 1, stopass); 
                }
            }
    }

enter image description here

Usage:

ShowCombination(new List<string>() {"a", "b"}, 2);

Output:

a0b0
a0b1
a0b2
a1b0
a1b1
a1b2
a2b0
a2b1
a2b2

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460018

This adds not much to @Daniel's answer, just the way you make your weight dynamic:

int weight = 2;
List<string> collection1 = new List<string>{ "a", "b" };
var collection2 = Enumerable.Range(0, weight + 1);
var combinations=from str1 in collection1 
                 from int1 in collection2 
                 from str2 in collection1 
                 from int2 in collection2 
                 select str1 + int1 + str2 + int2;

foreach (var combi in combinations)
    Console.WriteLine(combi);

Edit: If you want all permutations, have a look at this project to see how it's implemented. It's working great.

http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

For example:

List<string> collection1 = new List<string> { "a", "b", "c", "d" };
var collection2 = Enumerable.Range(0, weight + 1);
collection1 = collection1.Concat(collection2.Select(i => i.ToString())).ToList();

var permutations = new Facet.Combinatorics.Permutations<String>(collection1);
foreach (IList<String> p in permutations)
{
    String combi = String.Join("", p);
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

If you truly need all possible combinations, use this:

List<string> collection = new List<string> {"a","b"};
List<int> numbers = new List<int> { 0, 1, 2 };
var result = 
from a in collection
from b in collection
from n1 in numbers
from n2 in numbers
select a + n1 + b + n2;

This results in 36 items: a0a0, a0a1, a0a2, a1a0, a1a1, a1a2, a2a0, a2a1, a2a2, a0b0, a0b1, a0b2, a1b0, a1b1, a1b2, a2b0, a2b1, a2b2, b0a0, b0a1, b0a2, b1a0, b1a1, b1a2, b2a0, b2a1, b2a2, b0b0, b0b1, b0b2, b1b0, b1b1, b1b2, b2b0, b2b1, b2b2

If you only need the combinations you stated in your question, use this:

List<int> numbers = new List<int> { 0, 1, 2 };
var result = 
from n1 in numbers
from n2 in numbers
select "a" + n1 + "b" + n2;

This results in only 9 items: a0b0, a0b1, a0b2, a1b0, a1b1, a1b2, a2b0, a2b1, a2b2

Upvotes: 3

Related Questions