MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / combination_lists

Function combination_lists

backtracking/all_combinations.py:13–20  ·  view source on GitHub ↗

Generates all possible combinations of k numbers out of 1 ... n using itertools. >>> combination_lists(n=4, k=2) [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

(n: int, k: int)

Source from the content-addressed store, hash-verified

11
12
13def combination_lists(n: int, k: int) -> list[list[int]]:
14 """
15 Generates all possible combinations of k numbers out of 1 ... n using itertools.
16
17 >>> combination_lists(n=4, k=2)
18 [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
19 """
20 return [list(x) for x in combinations(range(1, n + 1), k)]
21
22
23def generate_all_combinations(n: int, k: int) -> list[list[int]]:

Callers 1

Calls 1

combinationsFunction · 0.50

Tested by

no test coverage detected