Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. static IEnumerable<IEnumerable<T>>
  2. GetKCombs<T>(IEnumerable<T> list, int length) where T : IComparable
  3. {
  4. if (length == 1) return list.Select(t => new T[] { t });
  5. return GetKCombs(list, length - 1)
  6. .SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0),
  7. (t1, t2) => t1.Concat(new T[] { t2 }));
  8. }
  9.  
  10. string[] files = Directory.GetFiles(@"C:UsersDownloadsSamples", "*.txt");
  11. IEnumerable<IEnumerable<string>> filescombination = GetKCombs(files, 2);
  12.  
  13. foreach(var x in filescombination)
  14. {
  15. Console.WriteLine(x);
  16. }
  17.  
  18. ?x
  19. {System.Linq.Enumerable.ConcatIterator<string>}
  20. first: null
  21. second: null
  22.  
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. List<int> list = new List<int> { 1, 2, 3, 4 };
  28.  
  29. IEnumerable<IEnumerable<int>> result = GetKCombs(list, 2);
  30.  
  31. foreach (var line in result)
  32. {
  33. foreach (var item in line)
  34. {
  35. Console.Write("{0}, ", item);
  36. }
  37. Console.WriteLine();
  38. }
  39.  
  40. Console.ReadKey();
  41. }
  42.  
  43. static IEnumerable<IEnumerable<T>> GetKCombs<T>(IEnumerable<T> list, int length) where T : IComparable
  44. {
  45. if (length == 1) return list.Select(t => new T[] { t });
  46. return GetKCombs(list, length - 1)
  47. .SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0),
  48. (t1, t2) => t1.Concat(new T[] { t2 }));
  49. }
  50. }
  51.  
  52. foreach(IEnumerable<string> x in filescombination)
  53. {
  54. Console.WriteLine(x);
  55. }
  56.  
  57. foreach(var x in filescombination)
  58. {
  59. var y = x.ToList()[0] + ',' + x.ToList()[1];
  60. }
  61.  
  62. IEnumerable<string>
  63.  
  64. IEnumerable<IEnumerable<string>> filescombination = GetKCombs(files,2);
  65. List<string> flist = new List<string>();
  66. foreach(var x in filescombination)
  67. {
  68. var y = x.ToList()[0] + ',' + x.ToList()[1];
  69. flist.Add(y);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement