Skip to content

Instantly share code, notes, and snippets.

@dgroft
Created June 25, 2013 16:50
Show Gist options
  • Save dgroft/5860155 to your computer and use it in GitHub Desktop.
Save dgroft/5860155 to your computer and use it in GitHub Desktop.
Finds all permutations of a supplied list of generic values.
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