Skip to content

Commit 4b99885

Browse files
fixed_incorrect_test_combination_sum
1 parent c0da015 commit 4b99885

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

backtracking/combination_sum.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,26 @@ 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
"""
5256
path = [] # type: list[int]
5357
answer = [] # type: list[int]
58+
if not candidates:
59+
raise ValueError("Candidates list should not be empty")
60+
61+
if any(x < 0 for x in candidates):
62+
raise ValueError("All elements in candidates must be non-negative.")
63+
5464
backtrack(candidates, path, answer, target, 0)
5565
return answer
5666

5767

5868
def main() -> None:
59-
print(combination_sum([-8, 2.3, 0], 1))
69+
print(combination_sum([], 1))
6070

6171

6272
if __name__ == "__main__":

0 commit comments

Comments
 (0)