# Generate combinations (meh). def combinations(sequence, k): def inner(start, n, res): if n == k: yield tuple(res) else: for i in range(start, len(sequence)): res[n] = sequence[i] yield from inner(i+1, n+1, res) return inner(0, 0, [None] * k) # Show. n = 5 a = list(range(1, 1+n)) for k in range(1, 1+n): print(f'{n} choose {k}') for x in combinations(a, k): print('', x)
Standard input is empty
5 choose 1 (1,) (2,) (3,) (4,) (5,) 5 choose 2 (1, 2) (1, 3) (1, 4) (1, 5) (2, 3) (2, 4) (2, 5) (3, 4) (3, 5) (4, 5) 5 choose 3 (1, 2, 3) (1, 2, 4) (1, 2, 5) (1, 3, 4) (1, 3, 5) (1, 4, 5) (2, 3, 4) (2, 3, 5) (2, 4, 5) (3, 4, 5) 5 choose 4 (1, 2, 3, 4) (1, 2, 3, 5) (1, 2, 4, 5) (1, 3, 4, 5) (2, 3, 4, 5) 5 choose 5 (1, 2, 3, 4, 5)