Guest User

Untitled

a guest
Mar 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. List<int> A = new List<int> {1, 2, 3, 4, 5};
  2. List<int> B = new List<int> {0, 1};
  3. List<int> C = new List<int> {6};
  4. List<int> X = new List<int> {....,....};
  5.  
  6. 1-0-6
  7. 1-1-6
  8. 2-0-6
  9. 2-1-6
  10. 3-0-6
  11.  
  12. var combinations = from a in A
  13. from b in B
  14. from c in C
  15. orderby a, b, c
  16. select new List<int> { a, b, c };
  17.  
  18. var x = combinations.ToList();
  19.  
  20. var x = AllCombinationsOf(A, B, C);
  21.  
  22. public static List<List<T>> AllCombinationsOf<T>(params List<T>[] sets)
  23. {
  24. // need array bounds checking etc for production
  25. var combinations = new List<List<T>>();
  26.  
  27. // prime the data
  28. foreach (var value in sets[0])
  29. combinations.Add(new List<T> { value });
  30.  
  31. foreach (var set in sets.Skip(1))
  32. combinations = AddExtraSet(combinations, set);
  33.  
  34. return combinations;
  35. }
  36.  
  37. private static List<List<T>> AddExtraSet<T>
  38. (List<List<T>> combinations, List<T> set)
  39. {
  40. var newCombinations = from value in set
  41. from combination in combinations
  42. select new List<T>(combination) { value };
  43.  
  44. return newCombinations.ToList();
  45. }
  46.  
  47. var qry = from a in A
  48. from b in B
  49. from c in C
  50. select new {A=a,B=b,C=c};
  51.  
  52. static void Main() {
  53. List<List<int>> outerList = new List<List<int>>
  54. { new List<int>(){1, 2, 3, 4, 5},
  55. new List<int>(){0, 1},
  56. new List<int>(){6,3},
  57. new List<int>(){1,3,5}
  58. };
  59. int[] result = new int[outerList.Count];
  60. Recurse(result, 0, outerList);
  61. }
  62. static void Recurse<TList>(int[] selected, int index,
  63. IEnumerable<TList> remaining) where TList : IEnumerable<int> {
  64. IEnumerable<int> nextList = remaining.FirstOrDefault();
  65. if (nextList == null) {
  66. StringBuilder sb = new StringBuilder();
  67. foreach (int i in selected) {
  68. sb.Append(i).Append(',');
  69. }
  70. if (sb.Length > 0) sb.Length--;
  71. Console.WriteLine(sb);
  72. } else {
  73. foreach (int i in nextList) {
  74. selected[index] = i;
  75. Recurse(selected, index + 1, remaining.Skip(1));
  76. }
  77. }
  78. }
Add Comment
Please, Sign In to add comment