Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

combinations

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)


You'll only receive email when they publish something new.

More from 38617
All posts