From 6290a6264a32632ca0e576fd694a8b20105d138b Mon Sep 17 00:00:00 2001 From: moaldeen <132774635+moaldeen@users.noreply.github.com> Date: Wed, 15 Nov 2023 02:08:24 -0500 Subject: [PATCH 1/6] Update all_combinations.py The original implementation had limitations in handling edge cases and certain input parameters, leading to potential RecursionError. --- backtracking/all_combinations.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index ecbcc5882ec1..909eca256798 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -36,6 +36,8 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: ... for n in range(1, 6) for k in range(1, 6)) True """ + if k < 0 or n < 0 or k > n: + return [] # Return an empty list for invalid input result: list[list[int]] = [] create_all_state(1, n, k, [], result) @@ -53,7 +55,7 @@ def create_all_state( total_list.append(current_list[:]) return - for i in range(increment, total_number - level + 2): + for i in range(increment, total_number + 1): current_list.append(i) create_all_state(i + 1, total_number, level - 1, current_list, total_list) current_list.pop() From dae4f8690cf001dd97e3dd75a33bf2a06f4734ef Mon Sep 17 00:00:00 2001 From: moaldeen <132774635+moaldeen@users.noreply.github.com> Date: Wed, 15 Nov 2023 02:22:26 -0500 Subject: [PATCH 2/6] Update all_combinations.py Added checks to handle cases where n or k are negative or where k is greater than n. In such scenarios, the function now returns an empty list, avoiding invalid recursive calls. --- backtracking/all_combinations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 909eca256798..0514cb58bb64 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -55,7 +55,7 @@ def create_all_state( total_list.append(current_list[:]) return - for i in range(increment, total_number + 1): + i in range(increment, total_number - level + 2): current_list.append(i) create_all_state(i + 1, total_number, level - 1, current_list, total_list) current_list.pop() From 60206f6bdee11a519f9f881c124fcd0acd49201d Mon Sep 17 00:00:00 2001 From: moaldeen <132774635+moaldeen@users.noreply.github.com> Date: Wed, 15 Nov 2023 02:26:05 -0500 Subject: [PATCH 3/6] Update error handling Added checks to handle cases where `n` or `k` are negative or where `k` is greater than `n`. In such scenarios, the function now returns an empty list, avoiding invalid recursive calls. --- backtracking/all_combinations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 0514cb58bb64..587c6509decb 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -55,7 +55,7 @@ def create_all_state( total_list.append(current_list[:]) return - i in range(increment, total_number - level + 2): + for i in range(increment, total_number - level + 2): current_list.append(i) create_all_state(i + 1, total_number, level - 1, current_list, total_list) current_list.pop() From 32a74df2bdde4a96ab6db7a5b9467e57e3992368 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 25 Nov 2023 14:18:54 +0100 Subject: [PATCH 4/6] Update backtracking/all_combinations.py --- backtracking/all_combinations.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 587c6509decb..a2ece4711ea2 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -36,8 +36,12 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: ... for n in range(1, 6) for k in range(1, 6)) True """ - if k < 0 or n < 0 or k > n: - return [] # Return an empty list for invalid input + if k < 0: + raise ValueError("k must not be negative") + if n < 0: + raise ValueError("n must not be negative") + if k > n: + raise ValueError("k must less that or equal to n") result: list[list[int]] = [] create_all_state(1, n, k, [], result) From e2990eaab859e714348a65633dbfb1b2544d1c82 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 25 Nov 2023 14:22:32 +0100 Subject: [PATCH 5/6] Update all_combinations.py --- backtracking/all_combinations.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index a2ece4711ea2..2cc9e5f02b8e 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -26,9 +26,15 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: >>> generate_all_combinations(n=10, k=-1) Traceback (most recent call last): ... - RecursionError: maximum recursion depth exceeded + ValueError: k must not be negative >>> generate_all_combinations(n=-1, k=10) - [] + Traceback (most recent call last): + ... + ValueError: n must not be negative + >>> generate_all_combinations(n=0, k=1) + Traceback (most recent call last): + ... + ValueError: k must be less than or equal to n >>> 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 @@ -41,7 +47,7 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: if n < 0: raise ValueError("n must not be negative") if k > n: - raise ValueError("k must less that or equal to n") + raise ValueError("k must be less than or equal to n") result: list[list[int]] = [] create_all_state(1, n, k, [], result) From 674901c7c88f8b356fdde38070a06746c83ae17c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 25 Nov 2023 14:28:45 +0100 Subject: [PATCH 6/6] Apply suggestions from code review --- backtracking/all_combinations.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 2cc9e5f02b8e..407304948c39 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -31,10 +31,6 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: Traceback (most recent call last): ... ValueError: n must not be negative - >>> generate_all_combinations(n=0, k=1) - Traceback (most recent call last): - ... - ValueError: k must be less than or equal to n >>> 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 @@ -46,8 +42,6 @@ def generate_all_combinations(n: int, k: int) -> list[list[int]]: raise ValueError("k must not be negative") if n < 0: raise ValueError("n must not be negative") - if k > n: - raise ValueError("k must be less than or equal to n") result: list[list[int]] = [] create_all_state(1, n, k, [], result)