Created
June 25, 2013 16:50
-
-
Save dgroft/5860155 to your computer and use it in GitHub Desktop.
Finds all permutations of a supplied list of generic values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<List<T>> GetPermutations<T>(List<T> values) | |
{ | |
if (values.Count <= 1) return new List<List<T>>() { values }; | |
var results = new List<List<T>>(); | |
var perms = GetPermutations(values.Skip(1).ToList()); | |
foreach (var perm in perms) | |
{ | |
foreach (int i in Enumerable.Range(0, perm.Count + 1)) | |
{ | |
List<T> list = new List<T>(); | |
list.AddRange(perm.Take(i)); | |
list.Add(values[0]); | |
list.AddRange(perm.Skip(i)); | |
results.Add(list); | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment