生成所有组合的字符串列表 [英] Generate all combinations for a list of strings
问题描述
我要生成的字符串列表的所有可能组合的列表(它实际上是对象的列表,但为了简单起见,我们将使用字符串)。我需要这个名单,这样我可以在一个单元测试测试所有可能的组合。
因此,举例来说,如果我有一个列表:
变种allValues =新列表与所述;串GT;(){A1,A2,A3,B1,B2,C 1}
我需要一个列表与LT;名单<串GT;>
像所有组合:
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
等等...
一个递归函数可能做到这一点让所有组合方式,但似乎比我想象的更难。
任何指针?
感谢您。
编辑:两种溶液,有或没有递归
公共类CombinationGenerator< T>
{
公共IEnumerable的<名单,LT; T>> ProduceWithRecursion(列表< T> allValues)
{
为(变量I = 0; I&下;(1 <<;&下; allValues.Count);我+ +)
{
收率返回ConstructSetFromBits(ⅰ)。选择(N =&GT; allValues [n])的了ToList();
}
} 私人的IEnumerable&LT; INT&GT; ConstructSetFromBits(int i)以
{
变种N = 0;
为(;!I = 0; I / = 2)
{
如果((I和!1)= 0)收率回报N;
Ñ++;
}
} 公开名单&LT;名单,LT; T&GT;&GT; ProduceWithoutRecursion(列表&LT; T&GT; allValues)
{
VAR收集=新的List&LT;名单,LT; T&GT;&GT;();
对(INT计数器= 0;计数器≤(1下;&下; allValues.Count)++计数器)
{
清单&LT; T&GT;组合=新的List&LT; T&GT;();
的for(int i = 0; I&LT; allValues.Count ++ I)
{
如果((计数器及(1下;&下;ⅰ))== 0)
combination.Add(allValues [I]);
} //做一些组合
collection.Add(组合);
}
返回集合;
}
}
您可以手动进行的,使用该n位二进制数自然对应于n元素集合的子集的事实。
私人的IEnumerable&LT; INT&GT; constructSetFromBits(int i)以
{
对于(INT N = 0;!I = 0; I / = 2,N ++)
{
如果((I和!1)= 0)
产生回报N;
}
}清单&LT;串GT; allValues =新的List&LT;串GT;()
{A1,A2,A3,B1,B2,C 1};私人的IEnumerable&LT;名单,LT;串GT;&GT; produceEnumeration()
{
对(INT I = 0; I&下;(1 <<;&下; allValues.Count);我+ +)
{
回报收益率
constructSetFromBits(ⅰ)。选择(N =&GT; allValues [n])的了ToList();
}
}公开名单&LT;名单,LT;串GT;&GT; produceList()
{
返回produceEnumeration()了ToList()。
}
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;
}
}
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();
}
这篇关于生成所有组合的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!