是否有可能创建一些Linq,生成一个包含一系列数字的所有可能组合的List?
如果输入"21",它将生成一个包含以下元素的列表:
list[0] = "21"
list[1] = "22"
list[2] = "11"
list[3] = "12"
Run Code Online (Sandbox Code Playgroud)
(不按顺序)
我知道你可以使用范围来做以下事情:
List<char> letterRange = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToList(); //97 - 122 + 1 = 26 letters/iterations
Run Code Online (Sandbox Code Playgroud)
从az生成字母表.但我似乎无法转移这些知识来制作组合发生器
我已经能够用以下代码弄清楚它,但它似乎太笨重了,我相信它可以用几行完成.它确实感觉我做的是一个糟糕的解决方案.
想象一下,GetAllCombinations("4321")如果有帮助,我已经打过电话
public static String[] GetAllCombinations(String s)
{
var combinations = new string[PossibleCombinations(s.Length)];
int n = PossibleCombinations(s.Length - 1);
for (int i = 0; i < s.Length; i++)
{
String sub;
String[] subs;
if (i == 0)
{
sub = s.Substring(1); //Get …Run Code Online (Sandbox Code Playgroud) 我有一套必须安排的产品.有P个产品,每个产品从1到P索引.每个产品可以安排到0到T的时间段.我需要构建满足以下约束的产品计划的所有排列:
If p1.Index > p2.Index then p1.Schedule >= p2.Schedule.
Run Code Online (Sandbox Code Playgroud)
我正在努力构建迭代器.当产品数量是已知常量时,我知道如何通过LINQ执行此操作,但是当产品数量是输入参数时,我不确定如何生成此查询.
理想情况下,我想使用yield语法来构造此迭代器.
public class PotentialSchedule()
{
public PotentialSchedule(int[] schedulePermutation)
{
_schedulePermutation = schedulePermutation;
}
private readonly int[] _schedulePermutation;
}
private int _numberProducts = ...;
public IEnumerator<PotentialSchedule> GetEnumerator()
{
int[] permutation = new int[_numberProducts];
//Generate all permutation combinations here -- how?
yield return new PotentialSchedule(permutation);
}
Run Code Online (Sandbox Code Playgroud)
编辑:_numberProducts = 2时的示例
public IEnumerable<PotentialSchedule> GetEnumerator()
{
var query = from p1 in Enumerable.Range(0,T)
from p2 in Enumerable.Range(p2,T)
select new { P1 = p1, P2 = …Run Code Online (Sandbox Code Playgroud)