Skip to content

Commit 877fcd3

Browse files
committed
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
1 parent 678dedb commit 877fcd3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

backtracking/generate_parentheses_iterative.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def generate_parentheses_iterative(length: int) -> list:
1+
def generate_parentheses_iterative(length: int) -> list[str]:
22
"""
33
Generate all valid combinations of parentheses (Iterative Approach).
44
@@ -18,13 +18,18 @@ def generate_parentheses_iterative(length: int) -> list:
1818
1919
Returns:
2020
A list of strings representing valid combinations of parentheses
21+
22+
Raises:
23+
ValueError: If length is negative
24+
TypeError: If length is not an integer
2125
2226
Time Complexity:
23-
O(2^(2*length))
27+
O(4^n / sqrt(n)) - Catalan number growth
2428
2529
Space Complexity:
26-
O(2^(2*length))
30+
O(4^n / sqrt(n)) - Storage for all valid combinations
2731
32+
2833
>>> generate_parentheses_iterative(3)
2934
['()()()', '()(())', '(())()', '(()())', '((()))']
3035
>>> generate_parentheses_iterative(2)
@@ -33,22 +38,43 @@ def generate_parentheses_iterative(length: int) -> list:
3338
['()']
3439
>>> generate_parentheses_iterative(0)
3540
['']
41+
>>> generate_parentheses_iterative(-1)
42+
Traceback (most recent call last):
43+
...
44+
ValueError: length must be non-negative
45+
>>> generate_parentheses_iterative(2.5)
46+
Traceback (most recent call last):
47+
...
48+
TypeError: length must be an integer
3649
"""
37-
result = []
38-
stack = []
39-
50+
# Input validation
51+
if not isinstance(length, int):
52+
raise TypeError("length must be an integer")
53+
if length < 0:
54+
raise ValueError("length must be non-negative")
55+
56+
# Handle edge case
57+
if length == 0:
58+
return [""]
59+
60+
result: list[str] = []
61+
4062
# Each element in stack is a tuple (current_combination, open_count, close_count)
41-
stack.append(("", 0, 0))
63+
stack: list[tuple[str, int, int]] = [("", 0, 0)]
4264

4365
while stack:
4466
current_combination, open_count, close_count = stack.pop()
4567

68+
# If we've used all pairs, add to result
4669
if len(current_combination) == 2 * length:
4770
result.append(current_combination)
48-
71+
continue
72+
73+
# Add '(' if we haven't used all open parentheses
4974
if open_count < length:
5075
stack.append((current_combination + "(", open_count + 1, close_count))
51-
76+
77+
# Add ')' if it maintains validity
5278
if close_count < open_count:
5379
stack.append((current_combination + ")", open_count, close_count + 1))
5480

0 commit comments

Comments
 (0)