diff --git a/.amazonq/rules/problem-creation.md b/.amazonq/rules/problem-creation.md index 2fc2a30..a673a25 100644 --- a/.amazonq/rules/problem-creation.md +++ b/.amazonq/rules/problem-creation.md @@ -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 { diff --git a/.templates/leetcode/examples/README.md b/.templates/leetcode/examples/README.md index 6dbfef8..df7d99b 100644 --- a/.templates/leetcode/examples/README.md +++ b/.templates/leetcode/examples/README.md @@ -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) diff --git a/.templates/leetcode/examples/basic.json5 b/.templates/leetcode/examples/basic.json5 index c00a02f..391c7c6 100644 --- a/.templates/leetcode/examples/basic.json5 +++ b/.templates/leetcode/examples/basic.json5 @@ -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" } ], @@ -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" } diff --git a/.templates/leetcode/examples/design.json5 b/.templates/leetcode/examples/design.json5 index 9839fc7..dbe2096 100644 --- a/.templates/leetcode/examples/design.json5 +++ b/.templates/leetcode/examples/design.json5 @@ -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", diff --git a/.templates/leetcode/examples/linked_list.json5 b/.templates/leetcode/examples/linked_list.json5 deleted file mode 100644 index f0a77b2..0000000 --- a/.templates/leetcode/examples/linked_list.json5 +++ /dev/null @@ -1,78 +0,0 @@ -{ - // Linked List problem template - for linked list problems - // Example: Reverse Linked List II - // Key differences: ListNode imports, list-specific test setup - // NOTE: PascalCase naming - keep acronyms/Roman numerals ALL CAPS (LRUCache, ReverseLinkedListII) - - // === PROBLEM IDENTIFICATION === - problem_name: "reverse_linked_list_ii", // snake_case: used for directory/file names - solution_class_name: "Solution", // Always "Solution" for algorithm problems - problem_number: "92", // LeetCode problem number as string - problem_title: "Reverse Linked List II", // Exact title from LeetCode - difficulty: "Medium", // Easy, Medium, Hard - topics: "Linked List", // Linked list related topics - tags: ["grind-75"], // Optional: common problem set tags - - // === README CONTENT === - // IMPORTANT: Preserve rich HTML content from LeetCode including: - // - Code snippets with backticks: `code` - // - Bold text: **bold** or bold - // - Italic text: *italic* or italic - // - Images: tags with proper src and styling - // - HTML formatting:

,
,