Skip to content

Commit 6f2e656

Browse files
committed
feat: add ideal examples for ideal template
1 parent 6b1dc24 commit 6f2e656

File tree

414 files changed

+3747
-300
lines changed

Some content is hidden

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

414 files changed

+3747
-300
lines changed
Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
# Cookiecutter Template Generalization Plan
2+
3+
## Current Issues Identified
4+
5+
### 1. **Template Constraint Issues**
6+
7+
- `first_bad_version/solution.py`: Has `# TODO: template constraint` - needs custom `__init__` method
8+
- Template doesn't support constructor parameters or custom initialization logic
9+
- No support for API mocking patterns (like `isBadVersion`)
10+
11+
### 2. **Limited Method Flexibility**
12+
13+
- No support for static methods, class methods, or property decorators
14+
- Missing support for optional parameters with defaults
15+
- No support for method overloading patterns
16+
- Cannot handle methods that don't need `self` parameter
17+
18+
### 3. **Complex Test Patterns Not Supported**
19+
20+
- Design problems need operation sequences (LRU Cache pattern)
21+
- Interactive problems need API mocking (First Bad Version)
22+
- Some problems need custom helper methods in tests
23+
- No support for test fixtures or complex setup
24+
25+
### 4. **Playground Notebook Limitations**
26+
27+
- Fixed 4-cell structure doesn't fit all problem types
28+
- No support for visualization cells
29+
- Missing support for interactive debugging
30+
- Cannot handle complex setup requirements
31+
- **Import conflicts**: Cannot import `tests.py` due to root-level `tests/` directory conflict
32+
- **Missing test helpers**: Need access to test utility functions like `create_cycle_list` in notebooks
33+
- **Path manipulation required**: Manual `sys.path` modifications needed for imports
34+
35+
### 5. **Missing Template Variants**
36+
37+
- No support for multiple solution approaches in one file
38+
- Missing templates for specific problem categories (graph, trie, etc.)
39+
- No support for helper classes or data structures
40+
- Cannot generate algorithm explanation comments
41+
42+
## Proposed Solutions
43+
44+
### Phase 1: Template Structure Improvements
45+
46+
#### 1.1 Enhanced Method Configuration
47+
48+
```json
49+
"solution_methods": [
50+
{
51+
"name": "method_name",
52+
"parameters": "param1: type1, param2: type2 = default",
53+
"return_type": "ReturnType",
54+
"dummy_return": "default_value",
55+
"decorators": ["@staticmethod", "@classmethod", "@property"],
56+
"is_constructor": true,
57+
"time_complexity": "O(n)",
58+
"space_complexity": "O(1)",
59+
"algorithm_notes": "Brief explanation"
60+
}
61+
]
62+
```
63+
64+
#### 1.2 Flexible Template Structure
65+
66+
```jinja2
67+
{%- for method in solution_methods %}
68+
{%- if method.decorators %}
69+
{%- for decorator in method.decorators %}
70+
{{ decorator }}
71+
{%- endfor %}
72+
{%- endif %}
73+
# Time: {{ method.time_complexity or "O(?)" }}
74+
# Space: {{ method.space_complexity or "O(?)" }}
75+
{%- if method.is_constructor %}
76+
def {{ method.name }}(self{% if method.parameters %}, {{ method.parameters }}{% endif %}){% if method.return_type %} -> {{ method.return_type }}{% endif %}:
77+
{%- else %}
78+
def {{ method.name }}({% if not method.decorators or "@staticmethod" not in method.decorators %}self{% if method.parameters %}, {% endif %}{% endif %}{{ method.parameters or "" }}){% if method.return_type %} -> {{ method.return_type }}{% endif %}:
79+
{%- endif %}
80+
{%- if method.algorithm_notes %}
81+
# {{ method.algorithm_notes }}
82+
{%- endif %}
83+
# TODO: Implement {{ method.name }}
84+
{%- if method.dummy_return %}
85+
return {{ method.dummy_return }}
86+
{%- endif %}
87+
{%- endfor %}
88+
```
89+
90+
### Phase 2: Test Template Enhancements
91+
92+
#### 2.1 Flexible Test Patterns
93+
94+
```json
95+
"test_patterns": {
96+
"type": "basic|design|interactive|tree|graph",
97+
"setup_complexity": "simple|complex|custom",
98+
"assertion_type": "direct|tree_comparison|operation_sequence"
99+
}
100+
```
101+
102+
#### 2.2 Enhanced Test Methods
103+
104+
```json
105+
"test_methods": [
106+
{
107+
"name": "test_method",
108+
"pattern": "parametrized|custom|fixture",
109+
"setup_code": "custom setup if needed",
110+
"parametrize": "params",
111+
"test_cases": "test data",
112+
"body": "test logic",
113+
"cleanup_code": "optional cleanup"
114+
}
115+
]
116+
```
117+
118+
### Phase 3: Notebook Template Flexibility
119+
120+
#### 3.1 Dynamic Cell Structure
121+
122+
```json
123+
"notebook_cells": [
124+
{
125+
"id": "imports",
126+
"type": "code",
127+
"content": "{{ playground_imports }}"
128+
},
129+
{
130+
"id": "setup",
131+
"type": "code",
132+
"content": "{{ playground_setup }}"
133+
},
134+
{
135+
"id": "visualization",
136+
"type": "code",
137+
"content": "{{ playground_visualization }}",
138+
"optional": true
139+
},
140+
{
141+
"id": "execute",
142+
"type": "code",
143+
"content": "{{ playground_execution }}"
144+
},
145+
{
146+
"id": "test",
147+
"type": "code",
148+
"content": "{{ playground_assertion }}"
149+
}
150+
]
151+
```
152+
153+
#### 3.2 Test Helper Integration
154+
155+
```json
156+
"notebook_config": {
157+
"needs_test_helpers": true,
158+
"helper_functions": ["create_cycle_list", "build_tree", "create_graph"],
159+
"import_strategy": "helpers_file|inline_helpers|test_utils"
160+
}
161+
```
162+
163+
### Phase 4: Problem-Specific Templates
164+
165+
#### 4.1 Template Categories
166+
167+
- **Basic**: Array, string, number problems
168+
- **Design**: Data structure implementation (LRU Cache, Trie)
169+
- **Interactive**: Problems with external APIs (First Bad Version)
170+
- **Tree**: Binary tree problems with visualization
171+
- **Graph**: Graph problems with node structures
172+
- **Algorithm**: Complex algorithms with step-by-step breakdown
173+
174+
#### 4.2 Category-Specific Fields
175+
176+
```json
177+
"template_category": "design",
178+
"design_config": {
179+
"operation_methods": ["get", "put", "delete"],
180+
"test_operation_sequence": true,
181+
"supports_multiple_instances": true
182+
},
183+
"interactive_config": {
184+
"external_apis": ["isBadVersion"],
185+
"mock_setup_required": true
186+
},
187+
"tree_config": {
188+
"visualization_enabled": true,
189+
"supports_null_nodes": true
190+
}
191+
```
192+
193+
## Implementation Plan
194+
195+
### Step 1: Backup and Analysis (Week 1)
196+
197+
- [ ] Create backup of current template
198+
- [ ] Analyze all 70+ existing problems for patterns
199+
- [ ] Document current template limitations
200+
- [ ] Create test cases for new template features
201+
202+
### Step 2: Core Template Enhancement (Week 2)
203+
204+
- [ ] Implement enhanced method configuration
205+
- [ ] Add support for decorators and special methods
206+
- [ ] Update solution.py template with flexibility
207+
- [ ] Add algorithm complexity and notes support
208+
209+
### Step 3: Test Template Improvements (Week 3)
210+
211+
- [ ] Implement flexible test patterns
212+
- [ ] Add support for design problem test sequences
213+
- [ ] Add interactive problem mocking support
214+
- [ ] Update tests.py template
215+
216+
### Step 4: Notebook Template Enhancement (Week 4)
217+
218+
- [ ] Implement dynamic cell structure
219+
- [ ] Add visualization cell support
220+
- [ ] Add debugging and exploration cells
221+
- [ ] **Solve import conflicts**: Create `helpers.py` pattern for test utilities
222+
- [ ] **Clean imports**: Remove need for manual `sys.path` manipulation
223+
- [ ] **Test helper access**: Provide clean access to test utility functions
224+
- [ ] Update playground.ipynb template
225+
226+
### Step 5: Category-Specific Templates (Week 5)
227+
228+
- [ ] Create template variants for each category
229+
- [ ] Implement category-specific configuration
230+
- [ ] Add template selection logic to generator
231+
- [ ] Update JSON schema validation
232+
233+
### Step 6: Migration and Testing (Week 6)
234+
235+
- [ ] Test new template with existing problems
236+
- [ ] Create migration script for existing JSONs
237+
- [ ] Update documentation and examples
238+
- [ ] Validate all generated problems pass linting
239+
240+
## Success Criteria
241+
242+
1. **Template Flexibility**: Support all current problem types without manual fixes
243+
2. **Zero Manual Intervention**: Generated code should pass `make p-lint` immediately
244+
3. **Backward Compatibility**: Existing JSON files should work with new template
245+
4. **Enhanced Features**: Support for decorators, complex tests, and visualizations
246+
5. **Category Support**: Automatic template selection based on problem type
247+
248+
## Risk Mitigation
249+
250+
1. **Breaking Changes**: Maintain backward compatibility with existing JSONs
251+
2. **Complexity**: Keep template readable with clear documentation
252+
3. **Performance**: Ensure generation time remains fast
253+
4. **Maintenance**: Create comprehensive test suite for template validation
254+
255+
## Files to Modify
256+
257+
### Core Template Files
258+
259+
- `.templates/leetcode/{{cookiecutter.problem_name}}/solution.py`
260+
- `.templates/leetcode/{{cookiecutter.problem_name}}/tests.py`
261+
- `.templates/leetcode/{{cookiecutter.problem_name}}/playground.ipynb`
262+
- **Add**: `.templates/leetcode/{{cookiecutter.problem_name}}/helpers.py` (conditional)
263+
- `.templates/leetcode/cookiecutter.json`
264+
265+
### Generator Logic
266+
267+
- `leetcode_py/tools/generator.py`
268+
- `.templates/leetcode/gen.py`
269+
270+
### Examples and Documentation
271+
272+
- `.templates/leetcode/examples/basic.json5`
273+
- `.templates/leetcode/examples/design.json5`
274+
- Add: `.templates/leetcode/examples/interactive.json5`
275+
- Add: `.templates/leetcode/examples/tree.json5`
276+
- Add: `.templates/leetcode/examples/graph.json5`
277+
278+
### Validation and Testing
279+
280+
- Add: `.templates/leetcode/validate_template.py`
281+
- Add: `tests/templates/test_template_generation.py`
282+
283+
## Notebook Import Issue Solutions
284+
285+
### Option 1: Dedicated `helpers.py` File (Recommended)
286+
287+
- Generate `helpers.py` when `needs_test_helpers: true`
288+
- Extract reusable functions from test class
289+
- Clean imports: `from helpers import create_cycle_list`
290+
- No path manipulation needed
291+
292+
### Option 2: Enhanced Test Utils
293+
294+
- Add problem-specific helpers to `leetcode_py.test_utils`
295+
- Import: `from leetcode_py.test_utils import create_cycle_list`
296+
- Centralized but may become bloated
297+
298+
### Option 3: Inline Helper Functions
299+
300+
- Generate helper functions directly in notebook setup cell
301+
- Self-contained but duplicates code
302+
- Good for simple helpers
303+
304+
**Chosen Approach**: Option 1 with conditional generation based on problem type.
305+
306+
This plan addresses all identified template limitations while maintaining backward compatibility and adding powerful new features for different problem categories.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ lint:
4848

4949

5050
test:
51-
poetry run pytest leetcode/ tests/ \
51+
poetry run pytest leetcode_old/ leetcode_ideal/ tests/ \
5252
-v --cov=leetcode --cov=leetcode_py \
5353
--cov-report=term-missing \
5454
--cov-report=xml \

leetcode/combination_sum/playground.ipynb

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)