Skip to content

Commit e6b5d26

Browse files
Combination sum fix and test cases (TheAlgorithms#10193)
* fixed_incorrect_test_combination_sum * reverting fn arg * ruff * Update combination_sum.py * Update combination_sum.py --------- Co-authored-by: Maxim Smolskiy <[email protected]>
1 parent 0c39e43 commit e6b5d26

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

backtracking/combination_sum.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ def combination_sum(candidates: list, target: int) -> list:
4747
>>> combination_sum([-8, 2.3, 0], 1)
4848
Traceback (most recent call last):
4949
...
50-
RecursionError: maximum recursion depth exceeded
50+
ValueError: All elements in candidates must be non-negative
51+
>>> combination_sum([], 1)
52+
Traceback (most recent call last):
53+
...
54+
ValueError: Candidates list should not be empty
5155
"""
56+
if not candidates:
57+
raise ValueError("Candidates list should not be empty")
58+
59+
if any(x < 0 for x in candidates):
60+
raise ValueError("All elements in candidates must be non-negative")
61+
5262
path = [] # type: list[int]
5363
answer = [] # type: list[int]
5464
backtrack(candidates, path, answer, target, 0)

0 commit comments

Comments
 (0)