1

I was looking for a way to get all unique permutations of a List by groups of 3. I found a lot of source code to download and complex algorithms.

I finally just came up with my own code that seems to work. Could someone please tell me why the following is not a good idea. I'm assuming there must be something I am overlooking due to all the complex solutions I found and mine seems too simple.

var combinations = from a in Samples
                   from b in Samples
                   from c in Samples
                   where (string)a.sample != (string)b.sample 
                   && (string)a.sample != (string)c.sample 
                   && (string)b.sample != (string)c.sample 
                   select new Rank
                   {
                       sample = a.sample,
                       sampleb = b.sample,
                       samplec = c.sample 
                   };

foreach (var combo in combinations)
{
    string[] aut = { combo.sample, combo.sampleb, combo.samplec };
    Array.Sort(aut);
    combo.sample = aut[0];
    combo.sampleb = aut[1];
    combo.samplec = aut[2];
    l.Add(combo);
}

noDupes = from n in l
          group n by new { n.sample, n.sampleb, n.samplec } into g
          select new Rank
          {
              sample = g.Key.sample,
              sampleb = g.Key.sampleb,
              samplec = g.Key.samplec
          };
4

1 回答 1

1

您正在添加所有内容,然后删除重复项。你不需要,看看这个(我的)关于排列的答案:https ://stackoverflow.com/a/9315076/360211

应用于您的方案,您有一个 3 位数字 ( maxDigits = 3),其中base = Samples.Count()

如果您的代码已经可以工作,那么这种方法的优势仅与性能有关。

于 2013-09-25T12:31:38.647 回答