Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
- {
- if (length == 1) return list.Select(t => new T[] { t });
- return GetPermutations(list, length - 1)
- .SelectMany(t => list.Where(e => !t.Contains(e)),
- (t1, t2) => t1.Concat(new T[] { t2 }));
- }
- static void Main(string[] args)
- {
- IEnumerable<IEnumerable<int>> result = GetPermutations(Enumerable.Range(1, 20), 20);
- foreach (var permutation in result )
- {
- Console.WriteLine(string.Join(" ", permutation));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement