From b8579618eb4685caf1b20c1ab2799034a56ded17 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 01:36:17 -0700 Subject: [PATCH 1/3] [ADD] Test cases for all_combinations --- backtracking/all_combinations.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index bde60f0328ba..4c8d39038ecb 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -1,7 +1,9 @@ """ In this problem, we want to determine all possible combinations of k numbers out of 1 ... n. We use backtracking to solve this problem. - Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))) + + Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))), + where n determines the range of numbers to combine and k represents the size of each arr generated """ from __future__ import annotations @@ -10,6 +12,12 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: """ >>> generate_all_combinations(n=4, k=2) [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] + >>> generate_all_combinations(n=0, k=0) + [[]] + >>> generate_all_combinations(n=10, k=0) + [[]] + >>> generate_all_combinations(n=5, k=4) + [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]] """ result: list[list[int]] = [] From ade2eae2fa1078365263dad20a8e85893d38d758 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 01:49:30 -0700 Subject: [PATCH 2/3] [DEL] documentation reverted b/c redundant --- backtracking/all_combinations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 4c8d39038ecb..37a5b6f88e7f 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -3,7 +3,6 @@ numbers out of 1 ... n. We use backtracking to solve this problem. Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))), - where n determines the range of numbers to combine and k represents the size of each arr generated """ from __future__ import annotations From e0929e7f4dbd719e2565f1475b291a83d633b1f5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 17 Oct 2023 16:54:03 +0200 Subject: [PATCH 3/3] Update all_combinations.py --- backtracking/all_combinations.py | 42 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 37a5b6f88e7f..ecbcc5882ec1 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -6,6 +6,16 @@ """ from __future__ import annotations +from itertools import combinations + + +def combination_lists(n: int, k: int) -> list[list[int]]: + """ + >>> combination_lists(n=4, k=2) + [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] + """ + return [list(x) for x in combinations(range(1, n + 1), k)] + def generate_all_combinations(n: int, k: int) -> list[list[int]]: """ @@ -13,10 +23,18 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] >>> generate_all_combinations(n=0, k=0) [[]] - >>> generate_all_combinations(n=10, k=0) - [[]] + >>> generate_all_combinations(n=10, k=-1) + Traceback (most recent call last): + ... + RecursionError: maximum recursion depth exceeded + >>> generate_all_combinations(n=-1, k=10) + [] >>> generate_all_combinations(n=5, k=4) [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]] + >>> from itertools import combinations + >>> all(generate_all_combinations(n, k) == combination_lists(n, k) + ... for n in range(1, 6) for k in range(1, 6)) + True """ result: list[list[int]] = [] @@ -41,13 +59,17 @@ def create_all_state( current_list.pop() -def print_all_state(total_list: list[list[int]]) -> None: - for i in total_list: - print(*i) +if __name__ == "__main__": + from doctest import testmod + testmod() + print(generate_all_combinations(n=4, k=2)) + tests = ((n, k) for n in range(1, 5) for k in range(1, 5)) + for n, k in tests: + print(n, k, generate_all_combinations(n, k) == combination_lists(n, k)) -if __name__ == "__main__": - n = 4 - k = 2 - total_list = generate_all_combinations(n, k) - print_all_state(total_list) + print("Benchmark:") + from timeit import timeit + + for func in ("combination_lists", "generate_all_combinations"): + print(f"{func:>25}(): {timeit(f'{func}(n=4, k = 2)', globals=globals())}")