Skip to content

Commit f164898

Browse files
authored
feat: update generic data structures (#17)
1 parent 041590d commit f164898

File tree

15 files changed

+126
-92
lines changed

15 files changed

+126
-92
lines changed

.templates/leetcode/examples/linked_list.json5

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
solution_methods: [
4242
{
4343
name: "reverse_between", // snake_case method name
44-
parameters: "head: ListNode | None, left: int, right: int", // Use ListNode | None for nullable parameters
45-
return_type: "ListNode | None", // Modern union syntax
44+
parameters: "head: ListNode[int] | None, left: int, right: int", // Use ListNode[int] | None for nullable parameters
45+
return_type: "ListNode[int] | None", // Modern union syntax with explicit generic type
4646
dummy_return: "None", // None for linked list problems
4747
},
4848
],
@@ -65,14 +65,14 @@
6565
parametrize_typed: "head_list: list[int], left: int, right: int, expected_list: list[int]",
6666
test_cases: "[([1, 2, 3, 4, 5], 2, 4, [1, 4, 3, 2, 5]), ([5], 1, 1, [5])]",
6767
// IMPORTANT: Linked list test body converts arrays to ListNode and compares objects directly
68-
body: "head = ListNode.from_list(head_list)\nexpected = ListNode.from_list(expected_list)\nresult = self.solution.reverse_between(head, left, right)\nassert result == expected",
68+
body: "head = ListNode[int].from_list(head_list)\nexpected = ListNode[int].from_list(expected_list)\nresult = self.solution.reverse_between(head, left, right)\nassert result == expected",
6969
},
7070
],
7171

7272
// === PLAYGROUND NOTEBOOK ===
7373
// IMPORTANT: Linked list playground needs ListNode import and conversion
7474
playground_imports: "from solution import Solution\n\nfrom leetcode_py import ListNode",
75-
playground_test_case: "# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode.from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode.from_list([1, 4, 3, 2, 5])",
75+
playground_test_case: "# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode[int].from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode[int].from_list([1, 4, 3, 2, 5])",
7676
playground_execution: "result = Solution().reverse_between(head, left, right)\nresult",
7777
playground_assertion: "assert result == expected",
7878
}

.templates/leetcode/examples/tree.json5

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
solution_methods: [
4646
{
4747
name: "invert_tree", // snake_case method name
48-
parameters: "root: TreeNode | None", // Use TreeNode | None for nullable tree parameters
49-
return_type: "TreeNode | None", // Modern union syntax (not Optional[TreeNode])
48+
parameters: "root: TreeNode[int] | None", // Use TreeNode[int] | None for nullable tree parameters
49+
return_type: "TreeNode[int] | None", // Modern union syntax with explicit generic type
5050
dummy_return: "None", // None for tree problems
5151
},
5252
],
@@ -69,14 +69,14 @@
6969
parametrize_typed: "root_list: list[int | None], expected_list: list[int | None]",
7070
test_cases: "[([4, 2, 7, 1, 3, 6, 9], [4, 7, 2, 9, 6, 3, 1]), ([2, 1, 3], [2, 3, 1]), ([], [])]",
7171
// IMPORTANT: Tree test body converts arrays to TreeNode and compares objects directly
72-
body: "root = TreeNode.from_list(root_list)\nexpected = TreeNode.from_list(expected_list)\nresult = self.solution.invert_tree(root)\nassert result == expected",
72+
body: "root = TreeNode[int].from_list(root_list)\nexpected = TreeNode[int].from_list(expected_list)\nresult = self.solution.invert_tree(root)\nassert result == expected",
7373
},
7474
],
7575

7676
// === PLAYGROUND NOTEBOOK ===
7777
// IMPORTANT: Tree playground needs TreeNode import and conversion
7878
playground_imports: "from solution import Solution\n\nfrom leetcode_py import TreeNode",
79-
playground_test_case: "# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode.from_list(root_list)\nexpected = TreeNode.from_list([4, 7, 2, 9, 6, 3, 1])",
79+
playground_test_case: "# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode[int].from_list(root_list)\nexpected = TreeNode[int].from_list([4, 7, 2, 9, 6, 3, 1])",
8080
playground_execution: "result = Solution().invert_tree(root)\nresult",
8181
playground_assertion: "assert result == expected",
8282
}

.templates/leetcode/json/invert_binary_tree.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"solution_methods": [
1919
{
2020
"name": "invert_tree",
21-
"parameters": "root: TreeNode | None",
22-
"return_type": "TreeNode | None",
21+
"parameters": "root: TreeNode[int] | None",
22+
"return_type": "TreeNode[int] | None",
2323
"dummy_return": "None"
2424
}
2525
],
@@ -34,11 +34,11 @@
3434
"parametrize": "root_list, expected_list",
3535
"parametrize_typed": "root_list: list[int | None], expected_list: list[int | None]",
3636
"test_cases": "[([4, 2, 7, 1, 3, 6, 9], [4, 7, 2, 9, 6, 3, 1]), ([2, 1, 3], [2, 3, 1]), ([], [])]",
37-
"body": "root = TreeNode.from_list(root_list)\nexpected = TreeNode.from_list(expected_list)\nresult = self.solution.invert_tree(root)\nassert result == expected"
37+
"body": "root = TreeNode[int].from_list(root_list)\nexpected = TreeNode[int].from_list(expected_list)\nresult = self.solution.invert_tree(root)\nassert result == expected"
3838
}
3939
],
4040
"playground_imports": "from solution import Solution\n\nfrom leetcode_py import TreeNode",
41-
"playground_test_case": "# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode.from_list(root_list)\nexpected = TreeNode.from_list([4, 7, 2, 9, 6, 3, 1])",
41+
"playground_test_case": "# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode[int].from_list(root_list)\nexpected = TreeNode[int].from_list([4, 7, 2, 9, 6, 3, 1])",
4242
"playground_execution": "result = Solution().invert_tree(root)\nresult",
4343
"playground_assertion": "assert result == expected"
4444
}

.templates/leetcode/json/reverse_linked_list_ii.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"solution_methods": [
1818
{
1919
"name": "reverse_between",
20-
"parameters": "head: ListNode | None, left: int, right: int",
21-
"return_type": "ListNode | None",
20+
"parameters": "head: ListNode[int] | None, left: int, right: int",
21+
"return_type": "ListNode[int] | None",
2222
"dummy_return": "None"
2323
}
2424
],
@@ -33,11 +33,11 @@
3333
"parametrize": "head_list, left, right, expected_list",
3434
"parametrize_typed": "head_list: list[int], left: int, right: int, expected_list: list[int]",
3535
"test_cases": "[([1, 2, 3, 4, 5], 2, 4, [1, 4, 3, 2, 5]), ([5], 1, 1, [5])]",
36-
"body": "head = ListNode.from_list(head_list)\nexpected = ListNode.from_list(expected_list)\nresult = self.solution.reverse_between(head, left, right)\nassert result == expected"
36+
"body": "head = ListNode[int].from_list(head_list)\nexpected = ListNode[int].from_list(expected_list)\nresult = self.solution.reverse_between(head, left, right)\nassert result == expected"
3737
}
3838
],
3939
"playground_imports": "from solution import Solution\n\nfrom leetcode_py import ListNode",
40-
"playground_test_case": "# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode.from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode.from_list([1, 4, 3, 2, 5])",
40+
"playground_test_case": "# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode[int].from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode[int].from_list([1, 4, 3, 2, 5])",
4141
"playground_execution": "result = Solution().reverse_between(head, left, right)\nresult",
4242
"playground_assertion": "assert result == expected"
4343
}

leetcode/invert_binary_tree/playground.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"id": "setup",
1515
"metadata": {},
1616
"outputs": [],
17-
"source": ["# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode.from_list(root_list)\nexpected = TreeNode.from_list([4, 7, 2, 9, 6, 3, 1])"]
17+
"source": ["# Example test case\nroot_list: list[int | None] = [4, 2, 7, 1, 3, 6, 9]\nroot = TreeNode[int].from_list(root_list)\nexpected = TreeNode[int].from_list([4, 7, 2, 9, 6, 3, 1])"]
1818
},
1919
{
2020
"cell_type": "code",

leetcode/invert_binary_tree/solution.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution:
1010
# DFS recursive
1111
# Time: O(n)
1212
# Space: O(h) where h is height of tree
13-
def invert_tree(self, root: TreeNode | None) -> TreeNode | None:
13+
def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
1414
if not root:
1515
return None
1616

@@ -22,11 +22,11 @@ class SolutionDFS:
2222
# DFS iterative
2323
# Time: O(n)
2424
# Space: O(h) where h is height of tree
25-
def invert_tree(self, root: TreeNode | None) -> TreeNode | None:
25+
def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
2626
if not root:
2727
return None
2828

29-
stack: list[TreeNode | None] = [root]
29+
stack: list[TreeNode[int] | None] = [root]
3030
while stack:
3131
node = stack.pop()
3232
if node is None:
@@ -42,11 +42,11 @@ def invert_tree(self, root: TreeNode | None) -> TreeNode | None:
4242
class SolutionBFS:
4343
# Time: O(n)
4444
# Space: O(w) where w is maximum width of tree
45-
def invert_tree(self, root: TreeNode | None) -> TreeNode | None:
45+
def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
4646
if not root:
4747
return None
4848

49-
queue: deque[TreeNode | None] = deque([root])
49+
queue: deque[TreeNode[int] | None] = deque([root])
5050
while queue:
5151
node = queue.popleft()
5252
if node is None:

leetcode/invert_binary_tree/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_invert_tree(
2020
solution_class: type[Solution | SolutionDFS | SolutionBFS],
2121
):
2222
solution = solution_class()
23-
root = TreeNode.from_list(root_list)
24-
expected = TreeNode.from_list(expected_list)
23+
root = TreeNode[int].from_list(root_list)
24+
expected = TreeNode[int].from_list(expected_list)
2525
result = solution.invert_tree(root)
2626
assert result == expected

leetcode/lru_cache/solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class LRUCache:
55
# Space: O(capacity)
6-
def __init__(self, capacity: int):
6+
def __init__(self, capacity: int) -> None:
77
self.capacity = capacity
88
self.cache: OrderedDict[int, int] = OrderedDict()
99

leetcode/reverse_linked_list_ii/playground.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"id": "setup",
1515
"metadata": {},
1616
"outputs": [],
17-
"source": ["# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode.from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode.from_list([1, 4, 3, 2, 5])"]
17+
"source": ["# Example test case\nhead_list = [1, 2, 3, 4, 5]\nhead = ListNode[int].from_list(head_list)\nleft, right = 2, 4\nexpected = ListNode[int].from_list([1, 4, 3, 2, 5])"]
1818
},
1919
{
2020
"cell_type": "code",

leetcode/reverse_linked_list_ii/solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
class Solution:
55
# Time: O(n)
66
# Space: O(1)
7-
def reverse_between(self, head: ListNode | None, left: int, right: int) -> ListNode | None:
7+
def reverse_between(self, head: ListNode[int] | None, left: int, right: int) -> ListNode[int] | None:
88
if not head or left == right:
99
return head
1010

11-
dummy = ListNode(0)
11+
dummy = ListNode[int](0)
1212
dummy.next = head
1313
prev = dummy
1414

0 commit comments

Comments
 (0)