combinations
June 5, 2025•72 words
Return all 3-combinations of the n-set
def bottom_up(n):
t = []
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
print((i,j,k))
t.append((i,j,k))
return t
for n=13 -> 286 13!/3!10! = 111213 / 6 = 286 (number of 3-combinations of 13 elements)
Q: Can we generalize this by making k parameter of the function?
A: Yes, itertools.combinations(range(n), k)