Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1.  
  2.  
  3.         static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
  4.         {
  5.             if (length == 1) return list.Select(t => new T[] { t });
  6.  
  7.             return GetPermutations(list, length - 1)
  8.                 .SelectMany(t => list.Where(e => !t.Contains(e)),
  9.                     (t1, t2) => t1.Concat(new T[] { t2 }));
  10.         }
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             IEnumerable<IEnumerable<int>> result = GetPermutations(Enumerable.Range(1, 20), 20);
  15.  
  16.             foreach (var permutation in result )
  17.             {
  18.                 Console.WriteLine(string.Join(" ", permutation));
  19.             }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement