Skip to content

Commit 238ec7b

Browse files
authored
feat: add tree_node (#7)
1 parent 3d2ba59 commit 238ec7b

File tree

19 files changed

+409
-139
lines changed

19 files changed

+409
-139
lines changed

.amazonq/rules/development-rules.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
- Use snake_case for Python method names (following Python convention)
1515
- Always include type hints for function parameters and return types
16+
- Use PEP 585/604 syntax: `list[str]`, `dict[str, int]`, `Type | None`, etc.
1617
- Add return statements to satisfy type checkers even if unreachable
1718
- Follow the project's linting rules (black, isort, ruff, mypy)
1819

@@ -26,6 +27,13 @@
2627
- `{parameters}` - method parameters with types
2728
- `{return_type}` - return type annotation
2829
- Test case placeholders with actual examples
30+
- **Template Implementation**: Do NOT implement the Solution class - only provide test cases and structure
31+
- **Helper Functions/Classes**: If the question relies on underlying helper functions or classes (e.g., TreeNode, ListNode):
32+
- First check if implementation already exists in `leetcode_py/common/` directory
33+
- If found, import from common module
34+
- If not found, create shared implementation in `leetcode_py/common/` for reusable classes
35+
- For question-specific helpers, implement directly in the solution file
36+
- **Update Makefile**: When adding new question, update the default `QUESTION` value in Makefile to the new question name
2937
- Always use the template structure for consistency
3038

3139
## File Structure

.codecov.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 70%
6+
patch:
7+
default:
8+
target: 70%

.github/workflows/ci-test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ on:
44
push:
55
branches: [main]
66
pull_request:
7-
branches: [main]
7+
types: [opened, synchronize, reopened]
88

99
jobs:
1010
test:
1111
runs-on: ubuntu-latest
1212

1313
steps:
14-
- uses: actions/checkout@v4
14+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
1515

1616
- name: Set up Python
17-
uses: actions/setup-python@v5
17+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
1818
with:
1919
python-version: "3.13"
2020

2121
- name: Install Poetry
22-
uses: snok/install-poetry@v1
22+
uses: snok/install-poetry@76e04a911780d5b312d89783f7b1cd627778900a # v1.4.1
2323
with:
2424
virtualenvs-create: true
2525
virtualenvs-in-project: true
2626
installer-parallel: true
2727

2828
- name: Load cached venv
2929
id: cached-poetry-dependencies
30-
uses: actions/cache@v4
30+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
3131
with:
3232
path: .venv
3333
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
PYTHON_VERSION = 3.13
2-
QUESTION ?= two_sum
2+
QUESTION ?= invert_binary_tree
33

44
sync_submodules:
55
git submodule update --init --recursive --remote
66

7+
# WARNING: Opinionated development setup for macOS + zsh + brew
8+
# This will install/update many tools: pipx, poetry, pre-commit, etc.
9+
# Only use if you understand what it does and accept the changes
710
setup_dev: sync_submodules
811
chmod +x scripts/shared/python/poetry/setup_dev.sh
912
./scripts/shared/python/poetry/setup_dev.sh \
@@ -42,4 +45,4 @@ test-question:
4245
echo "Error: Question '$(QUESTION)' not found in leetcode/ directory"; \
4346
exit 1; \
4447
fi
45-
cd leetcode/$(QUESTION) && poetry run pytest tests.py -v -s
48+
poetry run pytest leetcode/$(QUESTION)/tests.py -v -s

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# LeetCode Practice Repository 🚀
2+
3+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=wisarootl_leetcode-py&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=wisarootl_leetcode-py)
4+
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=wisarootl_leetcode-py&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=wisarootl_leetcode-py)
5+
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=wisarootl_leetcode-py&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=wisarootl_leetcode-py)
6+
[![codecov](https://codecov.io/gh/wisarootl/leetcode-py/graph/badge.svg?token=TI97VUIA4Z)](https://codecov.io/gh/wisarootl/leetcode-py)
7+
[![tests](https://img.shields.io/github/actions/workflow/status/wisarootl/leetcode-py/ci-test.yml?branch=main&label=tests&logo=github)](https://github.com/wisarootl/zerv/actions/workflows/ci-test.yml)
8+
[![release](https://img.shields.io/github/actions/workflow/status/wisarootl/leetcode-py/cd.yml?branch=main&label=release&logo=github)](https://github.com/wisarootl/zerv/actions/workflows/cd.yml)
9+
10+
Premium LeetCode practice environment with modern Python tooling, beautiful tree visualizations, and comprehensive testing.
11+
12+
## ✨ Features
13+
14+
- **Template-driven development** - Consistent structure for every problem
15+
- **Beautiful tree visualizations** - Pretty-printed binary trees with anytree
16+
- **Rich test logging** - `@logged_test` decorator with detailed tracebacks
17+
- **One-command testing** - `make test-question QUESTION=problem_name`
18+
- **Code quality** - black, isort, ruff, mypy integration
19+
- **Modern Python** - PEP 585/604 syntax with full type hints
20+
21+
## 🚀 Quick Start
22+
23+
```bash
24+
# Run existing problems
25+
make test-question QUESTION=two_sum
26+
make test-question QUESTION=invert_binary_tree
27+
28+
# Run all tests
29+
make test
30+
```
31+
32+
**Adding new problems**: Use an LLM agent (rules in `.amazonq/rules/development-rules.md`) to automatically create new problems from copied LeetCode problem text using the template structure.
33+
34+
## 🧰 Commands
35+
36+
```bash
37+
make test-question QUESTION=two_sum # Test specific problem
38+
make test # Run all tests
39+
make lint # Code quality checks
40+
```
41+
42+
## 🎨 Example Output
43+
44+
```
45+
# TreeNode visualization
46+
4
47+
├── 2
48+
│ ├── 1
49+
│ └── 3
50+
└── 7
51+
├── 6
52+
└── 9
53+
54+
# Test logging
55+
2024-01-01 10:00:00 | SUCCESS | Got result: [4,7,2,9,6,3,1]
56+
2024-01-01 10:00:00 | DEBUG | Test passed! ✨
57+
```
58+
59+
Perfect for interview preparation with professional-grade tooling and beautiful visualizations.

leetcode/_template/README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
**Difficulty:** {DIFFICULTY}
44
**Topics:** {TOPICS}
5+
**Tags:** {TAGS}
56

67
## Problem Description
78

@@ -39,13 +40,5 @@ Output: {OUTPUT_3}
3940

4041
{FOLLOW_UP}
4142

42-
## Solution
43-
44-
```python
45-
def {method_name}({parameters}):
46-
# TODO: Implement solution
47-
pass
48-
```
49-
5043
**Time Complexity:** O(?)
5144
**Space Complexity:** O(?)

leetcode/_template/solution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
class Solution:
5+
# Time: O(?)
6+
# Space: O(?)
57
def {method_name}(self, {parameters}) -> {return_type}:
68
# TODO: Implement solution
79
pass

leetcode/_template/tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from loguru import logger
3+
from leetcode_py.test_utils import logged_test
34
from .solution import Solution
45

56

@@ -15,9 +16,9 @@ def setup_method(self):
1516
({test_case_3}),
1617
],
1718
)
19+
@logged_test
1820
def test_{method_name}(self, {param_names}):
1921
logger.info(f"Testing with {input_description}")
2022
result = self.solution.{method_name}({input_params})
2123
logger.success(f"Got result: {result}")
2224
assert result == {expected_param}
23-
logger.debug("Test passed! ✨")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 226. Invert Binary Tree
2+
3+
**Difficulty:** Easy
4+
**Topics:** Tree, Depth-First Search, Breadth-First Search, Binary Tree
5+
**Tags:** grind-75
6+
7+
## Problem Description
8+
9+
Given the root of a binary tree, invert the tree, and return its root.
10+
11+
## Examples
12+
13+
### Example 1:
14+
15+
```
16+
Input: root = [4,2,7,1,3,6,9]
17+
Output: [4,7,2,9,6,3,1]
18+
```
19+
20+
### Example 2:
21+
22+
```
23+
Input: root = [2,1,3]
24+
Output: [2,3,1]
25+
```
26+
27+
### Example 3:
28+
29+
```
30+
Input: root = []
31+
Output: []
32+
```
33+
34+
## Constraints
35+
36+
- The number of nodes in the tree is in the range [0, 100].
37+
- -100 <= Node.val <= 100
38+
39+
**Time Complexity:** O(?)
40+
**Space Complexity:** O(?)

leetcode/invert_binary_tree/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)