Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all combinations for a list of strings

I want to generate a list of all possible combinations of a list of strings (it's actually a list of objects, but for simplicity we'll use strings). I need this list so that I can test every possible combination in a unit test.

So for example if I have a list of:

  var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" }

I need a List<List<string>> with all combinations like:

  A1
  A2
  A3
  B1
  B2
  C1
  A1 A2
  A1 A2 A3
  A1 A2 A3 B1
  A1 A2 A3 B1 B2
  A1 A2 A3 B1 B2 C1
  A1 A3
  A1 A3 B1
  etc...

A recursive function is probably the way to do it to get all combinations, but it seems harder than I imagined.

Any pointers?

Thank you.

EDIT: two solutions, with or without recursion:

public class CombinationGenerator<T>
{
    public IEnumerable<List<T>> ProduceWithRecursion(List<T> allValues) 
    {
        for (var i = 0; i < (1 << allValues.Count); i++)
        {
            yield return ConstructSetFromBits(i).Select(n => allValues[n]).ToList();
        }
    }

    private IEnumerable<int> ConstructSetFromBits(int i)
    {
        var n = 0;
        for (; i != 0; i /= 2)
        {
            if ((i & 1) != 0) yield return n;
            n++;
        }
    }

    public List<List<T>> ProduceWithoutRecursion(List<T> allValues)
    {
        var collection = new List<List<T>>();
        for (int counter = 0; counter < (1 << allValues.Count); ++counter)
        {
            List<T> combination = new List<T>();
            for (int i = 0; i < allValues.Count; ++i)
            {
                if ((counter & (1 << i)) == 0)
                    combination.Add(allValues[i]);
            }

            // do something with combination
            collection.Add(combination);
        }
        return collection;
    }
}
like image 812
L-Four Avatar asked May 09 '12 11:05

L-Four


People also ask

How do you get all the combinations of a string?

substring(1); Set<String> qq=find(st); for(String str:qq) { for(int i=0;i<=str. length();i++) { ss. add(comb(str,c,i)); } } } return ss; } public static String comb(String s,char c,int i) { String start=s. substring(0,i); String end=s.

How do you print all combinations of a list in Python?

combinations() module in Python to print all possible combinations. Given an array of size n, generate and print all possible combinations of r elements in array.

How do you generate all possible combinations of two lists?

Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!


2 Answers

You can make in manually, using the fact that n-bit binary number naturally corresponds to a subset of n-element set.

private IEnumerable<int> constructSetFromBits(int i)
{
    for (int n = 0; i != 0; i /= 2, n++)
    {
        if ((i & 1) != 0)
            yield return n;
    }
}

List<string> allValues = new List<string>()
        { "A1", "A2", "A3", "B1", "B2", "C1" };

private IEnumerable<List<string>> produceEnumeration()
{
    for (int i = 0; i < (1 << allValues.Count); i++)
    {
        yield return
            constructSetFromBits(i).Select(n => allValues[n]).ToList();
    }
}

public List<List<string>> produceList()
{
    return produceEnumeration().ToList();
}
like image 65
Vlad Avatar answered Oct 03 '22 02:10

Vlad


If you want all variations, have a look at this project to see how it's implemented.

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

But you can use it since it's open source under CPOL.

For example:

var allValues = new List<string>() { "A1", "A2", "A3", "B1", "B2", "C1" };
List<String> result = new List<String>();
var indices = Enumerable.Range(1, allValues.Count);
foreach (int lowerIndex in indices)
{
    var partVariations = new Facet.Combinatorics.Variations<String>(allValues, lowerIndex);
    result.AddRange(partVariations.Select(p => String.Join(" ", p)));
}

var length = result.Count;  // 1956
like image 40
Tim Schmelter Avatar answered Oct 03 '22 02:10

Tim Schmelter