From 877fcd33d4c0856c63447ab47554a761cce8d2c8 Mon Sep 17 00:00:00 2001 From: zain-cs Date: Wed, 11 Feb 2026 23:27:13 +0500 Subject: [PATCH 1/2] Improve generate_parentheses_iterative: Add input validation, type hints, and documentation - Added input validation for negative and non-integer inputs - Improved type hints (list[str] and tuple types) - Updated complexity analysis to Catalan number growth - Added doctests for error cases - Added explicit edge case handling for length=0 - Improved inline comments for clarity --- .../generate_parentheses_iterative.py | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index 175941c7ae95..53984a4fdd35 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -1,4 +1,4 @@ -def generate_parentheses_iterative(length: int) -> list: +def generate_parentheses_iterative(length: int) -> list[str]: """ Generate all valid combinations of parentheses (Iterative Approach). @@ -18,13 +18,18 @@ def generate_parentheses_iterative(length: int) -> list: Returns: A list of strings representing valid combinations of parentheses + + Raises: + ValueError: If length is negative + TypeError: If length is not an integer Time Complexity: - O(2^(2*length)) + O(4^n / sqrt(n)) - Catalan number growth Space Complexity: - O(2^(2*length)) + O(4^n / sqrt(n)) - Storage for all valid combinations + >>> generate_parentheses_iterative(3) ['()()()', '()(())', '(())()', '(()())', '((()))'] >>> generate_parentheses_iterative(2) @@ -33,22 +38,43 @@ def generate_parentheses_iterative(length: int) -> list: ['()'] >>> generate_parentheses_iterative(0) [''] + >>> generate_parentheses_iterative(-1) + Traceback (most recent call last): + ... + ValueError: length must be non-negative + >>> generate_parentheses_iterative(2.5) + Traceback (most recent call last): + ... + TypeError: length must be an integer """ - result = [] - stack = [] - + # Input validation + if not isinstance(length, int): + raise TypeError("length must be an integer") + if length < 0: + raise ValueError("length must be non-negative") + + # Handle edge case + if length == 0: + return [""] + + result: list[str] = [] + # Each element in stack is a tuple (current_combination, open_count, close_count) - stack.append(("", 0, 0)) + stack: list[tuple[str, int, int]] = [("", 0, 0)] while stack: current_combination, open_count, close_count = stack.pop() + # If we've used all pairs, add to result if len(current_combination) == 2 * length: result.append(current_combination) - + continue + + # Add '(' if we haven't used all open parentheses if open_count < length: stack.append((current_combination + "(", open_count + 1, close_count)) - + + # Add ')' if it maintains validity if close_count < open_count: stack.append((current_combination + ")", open_count, close_count + 1)) From 93bb327a23acc1cdf7666591c2e77f1bec29313c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Feb 2026 18:29:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/generate_parentheses_iterative.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/backtracking/generate_parentheses_iterative.py b/backtracking/generate_parentheses_iterative.py index 53984a4fdd35..918d809e92f0 100644 --- a/backtracking/generate_parentheses_iterative.py +++ b/backtracking/generate_parentheses_iterative.py @@ -18,7 +18,7 @@ def generate_parentheses_iterative(length: int) -> list[str]: Returns: A list of strings representing valid combinations of parentheses - + Raises: ValueError: If length is negative TypeError: If length is not an integer @@ -29,7 +29,7 @@ def generate_parentheses_iterative(length: int) -> list[str]: Space Complexity: O(4^n / sqrt(n)) - Storage for all valid combinations - + >>> generate_parentheses_iterative(3) ['()()()', '()(())', '(())()', '(()())', '((()))'] >>> generate_parentheses_iterative(2) @@ -52,13 +52,13 @@ def generate_parentheses_iterative(length: int) -> list[str]: raise TypeError("length must be an integer") if length < 0: raise ValueError("length must be non-negative") - + # Handle edge case if length == 0: return [""] - + result: list[str] = [] - + # Each element in stack is a tuple (current_combination, open_count, close_count) stack: list[tuple[str, int, int]] = [("", 0, 0)] @@ -69,11 +69,11 @@ def generate_parentheses_iterative(length: int) -> list[str]: if len(current_combination) == 2 * length: result.append(current_combination) continue - + # Add '(' if we haven't used all open parentheses if open_count < length: stack.append((current_combination + "(", open_count + 1, close_count)) - + # Add ')' if it maintains validity if close_count < open_count: stack.append((current_combination + ")", open_count, close_count + 1))