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

Function create_all_state

backtracking/all_combinations.py:64–100  ·  view source on GitHub ↗

Helper function to recursively build all combinations. >>> create_all_state(1, 4, 2, [], result := []) >>> result [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] >>> create_all_state(1, 3, 3, [], result := []) >>> result [[1, 2, 3]] >>> create_all_state(2, 2, 1, [1

(
    increment: int,
    total_number: int,
    level: int,
    current_list: list[int],
    total_list: list[list[int]],
)

Source from the content-addressed store, hash-verified

62
63
64def create_all_state(
65 increment: int,
66 total_number: int,
67 level: int,
68 current_list: list[int],
69 total_list: list[list[int]],
70) -> None:
71 """
72 Helper function to recursively build all combinations.
73
74 >>> create_all_state(1, 4, 2, [], result := [])
75 >>> result
76 [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
77 >>> create_all_state(1, 3, 3, [], result := [])
78 >>> result
79 [[1, 2, 3]]
80 >>> create_all_state(2, 2, 1, [1], result := [])
81 >>> result
82 [[1, 2]]
83 >>> create_all_state(1, 0, 0, [], result := [])
84 >>> result
85 [[]]
86 >>> create_all_state(1, 4, 0, [1, 2], result := [])
87 >>> result
88 [[1, 2]]
89 >>> create_all_state(5, 4, 2, [1, 2], result := [])
90 >>> result
91 []
92 """
93 if level == 0:
94 total_list.append(current_list[:])
95 return
96
97 for i in range(increment, total_number - level + 2):
98 current_list.append(i)
99 create_all_state(i + 1, total_number, level - 1, current_list, total_list)
100 current_list.pop()
101
102
103if __name__ == "__main__":

Callers 1

Calls 2

appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected