Skip to content

Commit f1a5c1f

Browse files
committed
feat: add complete backtracking problem implementations
- Fix solution comments to follow SOLUTION_CONTRACT.md A.7.2 format - Add JUDGE_FUNC for generator support to all 12 backtracking problems - Create test cases (.in/.out) for problems 0039, 0040, 0046, 0047, 0052, 0077, 0078, 0079, 0090, 0093, 0131, 0216 - Add generators following GENERATOR_CONTRACT.md - Update ontology with backtracking patterns and families
1 parent 5796f28 commit f1a5c1f

File tree

80 files changed

+1339
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1339
-0
lines changed

generators/0039_combination_sum.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# generators/0039_combination_sum.py
2+
"""
3+
Test Case Generator for Problem 0039 - Combination Sum
4+
5+
LeetCode Constraints:
6+
- 1 <= candidates.length <= 30
7+
- 2 <= candidates[i] <= 40
8+
- All elements of candidates are distinct
9+
- 1 <= target <= 40
10+
"""
11+
import random
12+
from typing import Iterator, Optional
13+
14+
15+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
16+
"""
17+
Generate test case inputs for Combination Sum.
18+
19+
Args:
20+
count: Number of test cases to generate
21+
seed: Random seed for reproducibility
22+
23+
Yields:
24+
str: Test input in the format: candidates\\ntarget
25+
"""
26+
if seed is not None:
27+
random.seed(seed)
28+
29+
# Edge cases first
30+
edge_cases = [
31+
"2,3,6,7\n7", # Classic example
32+
"2,3,5\n8", # Multiple combinations
33+
"2\n1", # No solution
34+
"7,8,9\n7", # Single element solution
35+
]
36+
37+
for edge in edge_cases:
38+
yield edge
39+
count -= 1
40+
if count <= 0:
41+
return
42+
43+
# Random cases
44+
for _ in range(count):
45+
yield _generate_case()
46+
47+
48+
def _generate_case() -> str:
49+
"""Generate a single random test case."""
50+
# Random number of candidates (2-15 for reasonable test size)
51+
n = random.randint(2, 15)
52+
53+
# Generate distinct candidates in range [2, 40]
54+
candidates = random.sample(range(2, 41), min(n, 39))
55+
56+
# Generate target that is likely achievable
57+
min_candidate = min(candidates)
58+
target = random.randint(min_candidate, 40)
59+
60+
candidates_str = ','.join(map(str, candidates))
61+
return f"{candidates_str}\n{target}"
62+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# generators/0040_combination_sum_ii.py
2+
"""
3+
Test Case Generator for Problem 0040 - Combination Sum II
4+
5+
LeetCode Constraints:
6+
- 1 <= candidates.length <= 100
7+
- 1 <= candidates[i] <= 50
8+
- 1 <= target <= 30
9+
"""
10+
import random
11+
from typing import Iterator, Optional
12+
13+
14+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
15+
"""
16+
Generate test case inputs for Combination Sum II.
17+
18+
Args:
19+
count: Number of test cases to generate
20+
seed: Random seed for reproducibility
21+
22+
Yields:
23+
str: Test input in the format: candidates\\ntarget
24+
"""
25+
if seed is not None:
26+
random.seed(seed)
27+
28+
# Edge cases first
29+
edge_cases = [
30+
"10,1,2,7,6,1,5\n8", # Classic with duplicates
31+
"2,5,2,1,2\n5", # Multiple duplicates
32+
"1,1,1,1,1\n3", # All same
33+
"2\n1", # No solution
34+
]
35+
36+
for edge in edge_cases:
37+
yield edge
38+
count -= 1
39+
if count <= 0:
40+
return
41+
42+
# Random cases
43+
for _ in range(count):
44+
yield _generate_case()
45+
46+
47+
def _generate_case() -> str:
48+
"""Generate a single random test case with possible duplicates."""
49+
# Random number of candidates (2-20 for reasonable test size)
50+
n = random.randint(2, 20)
51+
52+
# Generate candidates with possible duplicates
53+
candidates = [random.randint(1, 50) for _ in range(n)]
54+
55+
# Generate target in valid range
56+
target = random.randint(1, 30)
57+
58+
candidates_str = ','.join(map(str, candidates))
59+
return f"{candidates_str}\n{target}"
60+

generators/0046_permutations.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# generators/0046_permutations.py
2+
"""
3+
Test Case Generator for Problem 0046 - Permutations
4+
5+
LeetCode Constraints:
6+
- 1 <= nums.length <= 6
7+
- -10 <= nums[i] <= 10
8+
- All the integers of nums are unique
9+
"""
10+
import random
11+
from typing import Iterator, Optional
12+
13+
14+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
15+
"""
16+
Generate test case inputs for Permutations.
17+
18+
Args:
19+
count: Number of test cases to generate
20+
seed: Random seed for reproducibility
21+
22+
Yields:
23+
str: Test input in the format: nums (comma-separated)
24+
"""
25+
if seed is not None:
26+
random.seed(seed)
27+
28+
# Edge cases first
29+
edge_cases = [
30+
"1", # Single element
31+
"1,2", # Two elements
32+
"1,2,3", # Classic example
33+
"0,-1,1", # With negatives
34+
"1,2,3,4,5,6", # Maximum length
35+
]
36+
37+
for edge in edge_cases:
38+
yield edge
39+
count -= 1
40+
if count <= 0:
41+
return
42+
43+
# Random cases
44+
for _ in range(count):
45+
yield _generate_case()
46+
47+
48+
def _generate_case() -> str:
49+
"""Generate a single random test case with distinct integers."""
50+
# Random length 1-6
51+
n = random.randint(1, 6)
52+
53+
# Generate distinct integers in range [-10, 10]
54+
nums = random.sample(range(-10, 11), n)
55+
56+
return ','.join(map(str, nums))
57+

generators/0047_permutations_ii.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# generators/0047_permutations_ii.py
2+
"""
3+
Test Case Generator for Problem 0047 - Permutations II
4+
5+
LeetCode Constraints:
6+
- 1 <= nums.length <= 8
7+
- -10 <= nums[i] <= 10
8+
"""
9+
import random
10+
from typing import Iterator, Optional
11+
12+
13+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
14+
"""
15+
Generate test case inputs for Permutations II.
16+
17+
Args:
18+
count: Number of test cases to generate
19+
seed: Random seed for reproducibility
20+
21+
Yields:
22+
str: Test input in the format: nums (comma-separated)
23+
"""
24+
if seed is not None:
25+
random.seed(seed)
26+
27+
# Edge cases first
28+
edge_cases = [
29+
"1,1,2", # Classic with duplicates
30+
"1,2,3", # No duplicates
31+
"1,1,1", # All same
32+
"1,1,2,2", # Pairs of duplicates
33+
"0,0,0,0", # All zeros
34+
]
35+
36+
for edge in edge_cases:
37+
yield edge
38+
count -= 1
39+
if count <= 0:
40+
return
41+
42+
# Random cases
43+
for _ in range(count):
44+
yield _generate_case()
45+
46+
47+
def _generate_case() -> str:
48+
"""Generate a single random test case with possible duplicates."""
49+
# Random length 1-8
50+
n = random.randint(1, 8)
51+
52+
# Generate integers with possible duplicates
53+
nums = [random.randint(-10, 10) for _ in range(n)]
54+
55+
return ','.join(map(str, nums))
56+

generators/0052_n_queens_ii.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# generators/0052_n_queens_ii.py
2+
"""
3+
Test Case Generator for Problem 0052 - N-Queens II
4+
5+
LeetCode Constraints:
6+
- 1 <= n <= 9
7+
"""
8+
import random
9+
from typing import Iterator, Optional
10+
11+
12+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
13+
"""
14+
Generate test case inputs for N-Queens II.
15+
16+
Args:
17+
count: Number of test cases to generate
18+
seed: Random seed for reproducibility
19+
20+
Yields:
21+
str: Test input - a single integer n
22+
"""
23+
if seed is not None:
24+
random.seed(seed)
25+
26+
# Edge cases first (all valid n values)
27+
edge_cases = ["1", "4", "8", "9"]
28+
29+
for edge in edge_cases:
30+
yield edge
31+
count -= 1
32+
if count <= 0:
33+
return
34+
35+
# Random cases
36+
for _ in range(count):
37+
n = random.randint(1, 9)
38+
yield str(n)
39+

generators/0077_combinations.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# generators/0077_combinations.py
2+
"""
3+
Test Case Generator for Problem 0077 - Combinations
4+
5+
LeetCode Constraints:
6+
- 1 <= n <= 20
7+
- 1 <= k <= n
8+
"""
9+
import random
10+
from typing import Iterator, Optional
11+
12+
13+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
14+
"""
15+
Generate test case inputs for Combinations.
16+
17+
Args:
18+
count: Number of test cases to generate
19+
seed: Random seed for reproducibility
20+
21+
Yields:
22+
str: Test input in the format: n\\nk
23+
"""
24+
if seed is not None:
25+
random.seed(seed)
26+
27+
# Edge cases first
28+
edge_cases = [
29+
"4\n2", # Classic example
30+
"1\n1", # Minimal
31+
"5\n5", # k equals n
32+
"5\n1", # k equals 1
33+
"10\n3", # Larger case
34+
]
35+
36+
for edge in edge_cases:
37+
yield edge
38+
count -= 1
39+
if count <= 0:
40+
return
41+
42+
# Random cases
43+
for _ in range(count):
44+
yield _generate_case()
45+
46+
47+
def _generate_case() -> str:
48+
"""Generate a single random test case."""
49+
# Keep n reasonable to avoid explosion (C(20,10) is large)
50+
n = random.randint(1, 12)
51+
k = random.randint(1, n)
52+
return f"{n}\n{k}"
53+

generators/0078_subsets.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# generators/0078_subsets.py
2+
"""
3+
Test Case Generator for Problem 0078 - Subsets
4+
5+
LeetCode Constraints:
6+
- 1 <= nums.length <= 10
7+
- -10 <= nums[i] <= 10
8+
- All the numbers of nums are unique
9+
"""
10+
import random
11+
from typing import Iterator, Optional
12+
13+
14+
def generate(count: int = 10, seed: Optional[int] = None) -> Iterator[str]:
15+
"""
16+
Generate test case inputs for Subsets.
17+
18+
Args:
19+
count: Number of test cases to generate
20+
seed: Random seed for reproducibility
21+
22+
Yields:
23+
str: Test input in the format: nums (comma-separated)
24+
"""
25+
if seed is not None:
26+
random.seed(seed)
27+
28+
# Edge cases first
29+
edge_cases = [
30+
"1,2,3", # Classic example
31+
"0", # Single element
32+
"1,2", # Two elements
33+
"1,2,3,4", # Four elements
34+
]
35+
36+
for edge in edge_cases:
37+
yield edge
38+
count -= 1
39+
if count <= 0:
40+
return
41+
42+
# Random cases
43+
for _ in range(count):
44+
yield _generate_case()
45+
46+
47+
def _generate_case() -> str:
48+
"""Generate a single random test case with unique integers."""
49+
# Random length 1-10
50+
n = random.randint(1, 10)
51+
52+
# Generate distinct integers in range [-10, 10]
53+
nums = random.sample(range(-10, 11), n)
54+
55+
return ','.join(map(str, nums))
56+

0 commit comments

Comments
 (0)