fork download
  1. import numpy as np
  2.  
  3. def generate_combos(n, k, z):
  4. full_locs = np.arange(k + 1, dtype=np.uint)
  5. full_locs[k] = n # makes partial vectorization easier
  6. locs = full_locs[:k] # bit indices
  7. counts = np.zeros(n, dtype=np.uint) # counter buckets
  8. values = np.arange(n, dtype=np.uint) # values
  9. min_index = 0 # index of lowest non-exhausted bin
  10.  
  11. for _ in range((n * z) // k):
  12. counts[locs] += 1
  13. yield values[locs]
  14.  
  15. if counts[min_index] == z:
  16. # if lowest bin filled, shift and reset
  17. min_index += np.argmax(counts[min_index:] < z)
  18. locs[:] = min_index + np.arange(k)
  19. else:
  20. # otherwise, increment highest available counter
  21. i = np.flatnonzero(np.diff(full_locs))[-1]
  22. locs[i] += 1
  23. # reset the remainder
  24. locs[i + 1:] = locs[i] + np.arange(1, k - i)
  25.  
  26. for i, v in enumerate(generate_combos(256, 3, 10)):
  27. print(v)
  28. if i == 99:
  29. break
  30.  
  31.  
Success #stdin #stdout 0.23s 27264KB
stdin
Standard input is empty
stdout
[0 1 2]
[0 1 3]
[0 1 4]
[0 1 5]
[0 1 6]
[0 1 7]
[0 1 8]
[0 1 9]
[ 0  1 10]
[ 0  1 11]
[2 3 4]
[2 3 5]
[2 3 6]
[2 3 7]
[2 3 8]
[2 3 9]
[ 2  3 10]
[ 2  3 11]
[ 2  3 12]
[4 5 6]
[4 5 7]
[4 5 8]
[4 5 9]
[ 4  5 10]
[ 4  5 11]
[ 4  5 12]
[ 4  5 13]
[6 7 8]
[6 7 9]
[ 6  7 10]
[ 6  7 11]
[ 6  7 12]
[ 6  7 13]
[ 6  7 14]
[ 8  9 10]
[ 8  9 11]
[ 8  9 12]
[ 8  9 13]
[ 8  9 14]
[ 8  9 15]
[10 11 12]
[10 11 13]
[10 11 14]
[10 11 15]
[10 11 16]
[12 13 14]
[12 13 15]
[12 13 16]
[12 13 17]
[12 13 18]
[13 14 15]
[14 15 16]
[14 15 17]
[14 15 18]
[14 15 19]
[14 15 20]
[15 16 17]
[16 17 18]
[16 17 19]
[16 17 20]
[16 17 21]
[16 17 22]
[16 17 23]
[17 18 19]
[18 19 20]
[18 19 21]
[18 19 22]
[18 19 23]
[18 19 24]
[18 19 25]
[19 20 21]
[20 21 22]
[20 21 23]
[20 21 24]
[20 21 25]
[20 21 26]
[20 21 27]
[21 22 23]
[22 23 24]
[22 23 25]
[22 23 26]
[22 23 27]
[22 23 28]
[22 23 29]
[24 25 26]
[24 25 27]
[24 25 28]
[24 25 29]
[24 25 30]
[24 25 31]
[24 25 32]
[26 27 28]
[26 27 29]
[26 27 30]
[26 27 31]
[26 27 32]
[26 27 33]
[26 27 34]
[28 29 30]
[28 29 31]