Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .amazonq/rules/problem-creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ poetry run python .templates/leetcode/scrape.py -s "two-sum"

Required fields for `.templates/leetcode/json/{problem_name}.json`:

**CRITICAL: Use single quotes for Python strings in playground fields to avoid JSON escaping issues with Jupyter notebooks.**

**JSON Escaping Rules:**

- `playground_test_case`: Use single quotes for string literals (e.g., `s = 'hello'` not `s = "hello"`)
- `playground_execution`: Use single quotes for string literals
- `playground_assertion`: Use single quotes for string literals
- Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues

**Reference examples in `.templates/leetcode/examples/` for complete templates:**

- `basic.json5` - Array, string, number problems
- `basic.json5` - All standard problems (array, string, tree, linked list, etc.)
- `design.json5` - Data structure design problems (LRU Cache, etc.)
- `tree.json5` - Binary tree problems
- `linked_list.json5` - Linked list problems
- `matrix.json5` - 2D array/matrix problems

````json
{
Expand Down
136 changes: 30 additions & 106 deletions .templates/leetcode/examples/README.md
Original file line number Diff line number Diff line change
@@ -1,117 +1,41 @@
# LeetCode Problem Template Examples
# JSON Template Examples

This directory contains comprehensive JSON5 template examples for different types of LeetCode problems. These examples serve as references when creating new problems using the universal cookiecutter template.
This directory contains comprehensive examples for creating LeetCode problem templates.

## Template Types
## Files

### 1. `basic.json5` - Basic Algorithm Problems
- **`basic.json5`** - Covers all standard problem types:
- Array problems (Container With Most Water)
- String problems (with JSON escaping notes)
- Tree problems (import and parameter examples)
- Linked list problems (import and parameter examples)
- Matrix problems
- Number problems

**Use for:** Array, string, number, hash table problems
**Examples:** Container With Most Water, Two Sum, Valid Palindrome
**Key features:**
- **`design.json5`** - Data structure design problems:
- Custom class names (LRUCache, not Solution)
- Multiple methods including `__init__`
- Complex test setup with operation sequences
- Custom imports

- Simple `Solution` class with single method
- Standard test parametrization
- Basic playground setup
## Key Differences

### 2. `tree.json5` - Binary Tree Problems
### Standard Problems (basic.json5)

**Use for:** Binary tree, BST, tree traversal problems
**Examples:** Invert Binary Tree, Maximum Depth, Serialize Tree
**Key features:**
- `solution_class_name`: Always "Solution"
- Single method (usually)
- Simple test cases with direct assertions
- Standard imports

- `TreeNode` imports and conversions
- `TreeNode.from_list()` and `TreeNode.to_list()` in tests
- Tree visualization support
### Design Problems (design.json5)

### 3. `linked_list.json5` - Linked List Problems
- `solution_class_name`: Custom class name (e.g., "LRUCache")
- Multiple methods including constructor
- Operation sequence testing
- Import custom class in tests

**Use for:** Singly/doubly linked list problems
**Examples:** Reverse Linked List, Merge Lists, Detect Cycle
**Key features:**
## Critical Notes

- `ListNode` imports and conversions
- `ListNode.from_list()` and `ListNode.to_list()` in tests
- Arrow visualization support

### 4. `design.json5` - Data Structure Design Problems

**Use for:** Design problems requiring custom classes
**Examples:** LRU Cache, Implement Trie, Design HashMap
**Key features:**

- Custom class names (not `Solution`)
- Multiple methods including `__init__`
- Complex operation sequence testing
- Type annotations for complex test logic

### 5. `matrix.json5` - 2D Array/Matrix Problems

**Use for:** Matrix manipulation, 2D array problems
**Examples:** Spiral Matrix, Rotate Image, Search 2D Matrix
**Key features:**

- 2D array type annotations (`list[list[int]]`)
- Visual examples with images
- Matrix-specific test cases

## Usage Guidelines

### Problem Type Detection

1. **Basic**: Single algorithm, simple input/output
2. **Tree**: Mentions "tree", "node", uses tree terminology
3. **Linked List**: Mentions "linked list", "node", list operations
4. **Design**: "Design", "Implement", multiple operations
5. **Matrix**: "matrix", "2D array", "grid", visual layout

### Key Template Fields

#### Required Fields

- `problem_name`: snake_case identifier
- `solution_class_name`: "Solution" or custom class name
- `problem_number`: LeetCode number as string
- `problem_title`: Exact LeetCode title
- `difficulty`: "Easy", "Medium", or "Hard"
- `topics`: Comma-separated topic string
- `solution_methods`: Array of method definitions

#### Important Patterns

- **Type Hints**: Use modern syntax (`list[int]`, `dict[str, int]`, `Type | None`)
- **Method Names**: Always snake_case
- **Test Cases**: String representation of Python data structures
- **Imports**: Include necessary helper classes (TreeNode, ListNode)

#### PascalCase Naming Rules

For `solution_class_name` and `test_class_name` properties:

- **Acronyms**: Keep all caps ("LRUCache" not "LruCache")
- **Roman numerals**: Keep all caps ("ReverseLinkedListII" not "ReverseLinkedListIi")
- **Common patterns**: "BST", "DFS", "BFS", "API", "URL", "HTML", "JSON", "XML"

### Template Selection Process

1. Identify problem type from description/title
2. Choose appropriate template from examples
3. Customize fields for specific problem
4. Ensure imports match problem requirements
5. Verify test setup matches data structures used

## Validation

All templates are validated against:

- Cookiecutter template compatibility
- Linting requirements (black, isort, ruff, mypy)
- Test framework integration
- Notebook JSON format compliance

## Notes

- JSON5 format allows comments for documentation
- All examples are based on working, tested templates
- Templates are designed for the universal cookiecutter system
- Examples include both simple and complex problem patterns
- **JSON Escaping**: Use single quotes for Python strings in playground fields
- **Type Hints**: Use modern syntax (`list[int]`, `TreeNode | None`)
- **PascalCase**: Keep acronyms ALL CAPS (LRUCache, ReverseLinkedListII)
17 changes: 15 additions & 2 deletions .templates/leetcode/examples/basic.json5
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@
"readme_additional": "", // Optional: additional notes, follow-up questions

// === SOLUTION TEMPLATE ===
"solution_imports": "", // Empty for basic problems, add imports if needed
"solution_imports": "", // Empty for basic problems
// For tree: "from leetcode_py import TreeNode"
// For linked list: "from leetcode_py import ListNode"
"solution_methods": [
{
"name": "max_area", // snake_case method name
"parameters": "height: list[int]", // Modern Python type hints (list[int], not List[int])
// For tree: "root: TreeNode | None"
// For linked list: "head: ListNode | None"
// For string: "s: str"
"return_type": "int", // Return type annotation
"dummy_return": "0" // Default return value (auto-set by generator)
"dummy_return": "0" // Default return value
// For string: "\"\""
// For bool: "False"
// For list: "[]"
// For tree/linked list: "None"
}
],

Expand All @@ -68,8 +77,12 @@
],

// === PLAYGROUND NOTEBOOK ===
// CRITICAL: Use single quotes for Python strings to avoid JSON escaping issues with Jupyter notebooks
// Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues
"playground_imports": "from solution import Solution",
"playground_test_case": "# Example test case\nheight = [1,8,6,2,5,4,8,3,7]\nexpected = 49",
// For string problems: "s = 'hello'\nexpected = 'olleh'"
// For tree: "root_list = [3,9,20,None,None,15,7]\nroot = TreeNode.from_list(root_list)"
"playground_execution": "result = Solution().max_area(height)\nresult",
"playground_assertion": "assert result == expected"
}
3 changes: 2 additions & 1 deletion .templates/leetcode/examples/design.json5
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
],

// === PLAYGROUND NOTEBOOK ===
// IMPORTANT: Design playground uses operation sequences like tests
// CRITICAL: Use single quotes for Python strings to avoid JSON escaping issues with Jupyter notebooks
// Double quotes in JSON + cookiecutter + Jupyter notebook = triple escaping issues
playground_imports: "from solution import LRUCache",
playground_test_case: "# Example test case\noperations = ['LRUCache', 'put', 'put', 'get', 'put', 'get', 'put', 'get', 'get', 'get']\ninputs = [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]\nexpected = [None, None, None, 1, None, -1, None, -1, 3, 4]",
playground_execution: "cache = None\nresults: list[int | None] = []\nfor i, op in enumerate(operations):\n if op == 'LRUCache':\n cache = LRUCache(inputs[i][0])\n results.append(None)\n elif op == 'get' and cache is not None:\n results.append(cache.get(inputs[i][0]))\n elif op == 'put' and cache is not None:\n cache.put(inputs[i][0], inputs[i][1])\n results.append(None)\nresults",
Expand Down
78 changes: 0 additions & 78 deletions .templates/leetcode/examples/linked_list.json5

This file was deleted.

75 changes: 0 additions & 75 deletions .templates/leetcode/examples/matrix.json5

This file was deleted.

Loading
Loading