Linq的组合发电机

Cas*_*erT 19 c# linq combinations

是否有可能创建一些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 the first number
        }
        else if (i == s.Length - 1)
        {
            sub = s.Substring(0, s.Length - 1);
        }
        else
        {
            sub = s.Substring(0, i) + s.Substring(i + 1); 
        }

        subs = GetAllCombinations(sub);

        for (int j = 0; j < subs.Length; j++)
        {
            combinations[i * n + j] = s[i] + subs[j];
        }
    }

    return combinations;
}
public static int PossibleCombinations(int n) //Combination possibilities. e.g 1-2-3-4 have 24 different combinations
{
    int result = 1;

    for (int i = 1; i <= n; i++)
        result *= i;

    return result;
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*h G 39

为了它的价值,尝试这样的事情:

public static IEnumerable<string> GetPermutations(string s)
{
    if (s.Length > 1)
        return from ch in s
               from permutation in GetPermutations(s.Remove(s.IndexOf(ch), 1))
               select string.Format("{0}{1}", ch, permutation);

    else
        return new string[] { s };
}
Run Code Online (Sandbox Code Playgroud)

  • 需要注意的是,这个函数没有给出问题所要求的功能.(它生成`{"12","21"}`,缺少"11"和"22".)我只能假设提问者设法使其适应有用的东西. (3认同)

Mar*_*tke 32

为了记录:Josh回答了通用的方式:

public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items) {
        if (items.Count() > 1) {
            return items.SelectMany(item => GetPermutations(items.Where(i => !i.Equals(item))),
                                   (item, permutation) => new[] { item }.Concat(permutation));
        } else {
            return new[] {items};
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这需要更多的赞成.我总是喜欢通用答案! (8认同)
  • 需要注意的是:如果`items`中的两个或多个元素相等,则无效. (3认同)

Yon*_*Cho 10

这是我使用Linq的Permutation和Combination功能

public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source, TSource item)
{
    if (source == null)
        throw new ArgumentNullException("source");

    yield return item;

    foreach (var element in source)
        yield return element;
}

public static IEnumerable<IEnumerable<TSource>> Permutate<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    var list = source.ToList();

    if (list.Count > 1)
        return from s in list
                from p in Permutate(list.Take(list.IndexOf(s)).Concat(list.Skip(list.IndexOf(s) + 1)))
                select p.Prepend(s);

    return new[] { list };
}

public static IEnumerable<IEnumerable<TSource>> Combinate<TSource>(this IEnumerable<TSource> source, int k)
{
    if (source == null)
        throw new ArgumentNullException("source");

    var list = source.ToList();
    if (k > list.Count)
        throw new ArgumentOutOfRangeException("k");

    if (k == 0)
        yield return Enumerable.Empty<TSource>();

    foreach (var l in list)
        foreach (var c in Combinate(list.Skip(list.Count - k - 2), k - 1))
            yield return c.Prepend(l);
}
Run Code Online (Sandbox Code Playgroud)

对于DNA字母"A","C","G","T":

var dna = new[] {'A', 'C', 'G', 'T'};

foreach (var p in dna.Permutate())
    Console.WriteLine(String.Concat(p));
Run Code Online (Sandbox Code Playgroud)

ACGT ACTG AGCT AGTC ATCG ATGC CAGT CATG CGAT CGTA CTAG CTGA GACT GATC GCAT GCTA GTAC GTCA TACG TAGC TCAG TCGA TGAC TGCA
Run Code Online (Sandbox Code Playgroud)

和DNA字母表的组合(k = 2)

foreach (var c in dna.Combinate(2))
        Console.WriteLine(String.Concat(c));
Run Code Online (Sandbox Code Playgroud)

AA AC AG AT CA CC CG CT GA GC GG GT TA TC TG TT
Run Code Online (Sandbox Code Playgroud)