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
          };
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user2525463
  • 93
  • 1
  • 8
  • 1
    First ever question that I've seen asking "is my code too 1337?". Looks fine to me. Move on. – spender Sep 25 '13 at 12:23
  • Actually wanna be sure I'm not missing something. I'm fairly new at this. – user2525463 Sep 25 '13 at 12:25
  • Sir, yes sir. Sorry for interrupting your busy schedule of answering questions online. See you on ask.com later. @spender – user2525463 Sep 25 '13 at 12:28
  • Look at this (my) answer on permutations: http://stackoverflow.com/a/9315076/360211 – weston Sep 25 '13 at 12:29
  • 2
    Now, now, @user2525463... Linq allows you to write cartesian joins very tersely. Your solution looks fine. Apparently it works too. I just don't really see a proper question here. – spender Sep 25 '13 at 12:30
  • @weston thanks, is there any advantage to using that over what I have? – user2525463 Sep 25 '13 at 12:32
  • 3
    This question appears to be off-topic because it is about code-review. Ask such questions at http://codereview.stackexchange.com – Daniel Hilgarth Sep 25 '13 at 12:32
  • 2
    Depends on how large the samples are and how important performance is. – Tim Schmelter Sep 25 '13 at 12:33
  • @spender You answered it, "looks fine to me". Was just curious if there was a reason I shouldn't implement it this way or I was overlooking some permutation it may miss. Thank you for your time. – user2525463 Sep 25 '13 at 12:35

1 Answers1

1

You're adding all, then removing duplicates. You don't need to, see this (my) answer on permutations: https://stackoverflow.com/a/9315076/360211

Applying to your scenario, you have a 3 digit number (maxDigits = 3) where base = Samples.Count()

If your code already works, then the advantages of this approach are only performance related.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203