fork download
  1. # your code goes here
  2. import time
  3.  
  4. def rotate(l, n):
  5. return l[n:] + l[:n]
  6.  
  7. def numGroupCombs(n, nGrps, grpSize):
  8. result = 1
  9.  
  10. for i in range(n, nGrps, -1):
  11. result *= i
  12.  
  13. result = int(result)
  14. myDiv = 1
  15.  
  16. for i in range(2, grpSize + 1):
  17. myDiv *= i
  18.  
  19. result /= myDiv**nGrps
  20. return int(result)
  21.  
  22. def ComboGroups(v, nGrps, grpSize):
  23.  
  24. if not isinstance(v, list):
  25. z = list(v)
  26. else:
  27. z = v.copy()
  28.  
  29. for i in range(numGroupCombs(len(z), nGrps, grpSize)):
  30. yield z.copy()
  31.  
  32. idx1 = (nGrps - 1) * grpSize - 1
  33. idx2 = len(z) - 1;
  34. last1 = (nGrps - 2) * grpSize + 1
  35.  
  36. while (idx2 > idx1 and z[idx2] > z[idx1]):
  37. idx2 -= 1
  38.  
  39. if (idx2 + 1) < len(z):
  40. if z[idx2 + 1] > z[idx1]:
  41. z[idx1], z[idx2 + 1] = z[idx2 + 1], z[idx1]
  42. else:
  43. while idx1 > 0:
  44. tipPnt = z[idx2]
  45.  
  46. while (idx1 > last1 and tipPnt < z[idx1]):
  47. idx1 -= 1
  48.  
  49. if tipPnt > z[idx1]:
  50. idx3 = idx1 + 1
  51. z[idx3:] = sorted(z[idx3:])
  52. xtr = last1 + grpSize - idx3
  53.  
  54. while z[idx3] < z[idx1]:
  55. idx3 += 1
  56.  
  57. z[idx3], z[idx1] = z[idx1], z[idx3]
  58. z[(idx1 + 1):(idx3 + xtr)] = rotate(z[(idx1 + 1):(idx3 + xtr)], idx3 - idx1)
  59. break
  60. else:
  61. idx1 -= 2
  62. idx2 -= grpSize
  63. last1 -= grpSize
  64.  
  65. def example(z, nGrps, verbose = True):
  66. grpSize = int(len(z) / nGrps)
  67.  
  68. if verbose:
  69. for a in ComboGroups(z, nGrps, grpSize):
  70. print([a[i:i + grpSize] for i in range(0, len(a), grpSize)])
  71. else:
  72. start = time.time()
  73.  
  74. for a in ComboGroups(z, nGrps, grpSize):
  75. b = a
  76.  
  77. end = time.time()
  78. print(end - start)
  79.  
  80. example(list(range(1, 9)), 2)
  81. example(list(range(1, 19)), 3, False)
Success #stdin #stdout 4.87s 9188KB
stdin
Standard input is empty
stdout
[[1, 2, 3, 4], [5, 6, 7, 8]]
[[1, 2, 3, 5], [4, 6, 7, 8]]
[[1, 2, 3, 6], [4, 5, 7, 8]]
[[1, 2, 3, 7], [4, 5, 6, 8]]
[[1, 2, 3, 8], [4, 5, 6, 7]]
[[1, 2, 4, 5], [3, 6, 7, 8]]
[[1, 2, 4, 6], [3, 5, 7, 8]]
[[1, 2, 4, 7], [3, 5, 6, 8]]
[[1, 2, 4, 8], [3, 5, 6, 7]]
[[1, 2, 5, 6], [3, 4, 7, 8]]
[[1, 2, 5, 7], [3, 4, 6, 8]]
[[1, 2, 5, 8], [3, 4, 6, 7]]
[[1, 2, 6, 7], [3, 4, 5, 8]]
[[1, 2, 6, 8], [3, 4, 5, 7]]
[[1, 2, 7, 8], [3, 4, 5, 6]]
[[1, 3, 4, 5], [2, 6, 7, 8]]
[[1, 3, 4, 6], [2, 5, 7, 8]]
[[1, 3, 4, 7], [2, 5, 6, 8]]
[[1, 3, 4, 8], [2, 5, 6, 7]]
[[1, 3, 5, 6], [2, 4, 7, 8]]
[[1, 3, 5, 7], [2, 4, 6, 8]]
[[1, 3, 5, 8], [2, 4, 6, 7]]
[[1, 3, 6, 7], [2, 4, 5, 8]]
[[1, 3, 6, 8], [2, 4, 5, 7]]
[[1, 3, 7, 8], [2, 4, 5, 6]]
[[1, 4, 5, 6], [2, 3, 7, 8]]
[[1, 4, 5, 7], [2, 3, 6, 8]]
[[1, 4, 5, 8], [2, 3, 6, 7]]
[[1, 4, 6, 7], [2, 3, 5, 8]]
[[1, 4, 6, 8], [2, 3, 5, 7]]
[[1, 4, 7, 8], [2, 3, 5, 6]]
[[1, 5, 6, 7], [2, 3, 4, 8]]
[[1, 5, 6, 8], [2, 3, 4, 7]]
[[1, 5, 7, 8], [2, 3, 4, 6]]
[[1, 6, 7, 8], [2, 3, 4, 5]]
4.856988430023193