Skip to content

Commit 4495b03

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8f1d2c8 commit 4495b03

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

backtracking/combination_sum_2.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
1 <= target <= 30
1313
"""
1414

15-
def backtrack(candidates: list, target: int, start_index: int, total: int, path: list, answer: list) -> None:
15+
16+
def backtrack(
17+
candidates: list,
18+
target: int,
19+
start_index: int,
20+
total: int,
21+
path: list,
22+
answer: list,
23+
) -> None:
1624
"""
1725
A recursive function that searches for possible combinations. Backtracks in case
1826
of a bigger current combination value than the target value and removes the already
@@ -38,26 +46,31 @@ def backtrack(candidates: list, target: int, start_index: int, total: int, path:
3846
continue
3947
if total + candidates[i] > target:
4048
break
41-
backtrack(candidates, target, i + 1, total + candidates[i], path + [candidates[i]], answer)
42-
49+
backtrack(
50+
candidates,
51+
target,
52+
i + 1,
53+
total + candidates[i],
54+
path + [candidates[i]],
55+
answer,
56+
)
4357

4458

45-
46-
def combination_sum_2(candidates: list,target: int) -> list:
59+
def combination_sum_2(candidates: list, target: int) -> list:
60+
"""
61+
>>> combination_sum_2([10,1,2,7,6,1,5], 8)
62+
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
63+
>>> combination_sum_2([1,2], 2)
64+
[[2]]
65+
>>> combination_sum_2([-8, 2.3, 0], 1)
66+
Traceback (most recent call last):
67+
...
68+
ValueError: All elements in candidates must be non-negative
69+
>>> combination_sum_2([], 1)
70+
Traceback (most recent call last):
71+
...
72+
ValueError: Candidates list should not be empty
4773
"""
48-
>>> combination_sum_2([10,1,2,7,6,1,5], 8)
49-
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
50-
>>> combination_sum_2([1,2], 2)
51-
[[2]]
52-
>>> combination_sum_2([-8, 2.3, 0], 1)
53-
Traceback (most recent call last):
54-
...
55-
ValueError: All elements in candidates must be non-negative
56-
>>> combination_sum_2([], 1)
57-
Traceback (most recent call last):
58-
...
59-
ValueError: Candidates list should not be empty
60-
"""
6174
if not candidates:
6275
raise ValueError("Candidates list should not be empty")
6376

@@ -78,4 +91,4 @@ def main() -> None:
7891
import doctest
7992

8093
doctest.testmod()
81-
main()
94+
main()

0 commit comments

Comments
 (0)