Skip to content

Commit 4a20211

Browse files
Ensure all lines are <= 88 characters
1 parent 2e0a1f7 commit 4a20211

20 files changed

+116
-28
lines changed

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ dependencies = [
2020
"ruff"
2121
]
2222

23+
[tool.pytest.ini_options]
24+
filterwarnings = [
25+
"ignore:invalid escape sequence:SyntaxWarning"
26+
]
27+
28+
[tool.ruff]
29+
line-length = 88
30+
2331
[tool.ruff.lint]
2432
# A002: Argument is shadowing a Python builtin
2533
# E741: do not use variables named 'l', 'O', or 'I'

src/binary_tree_level_order_traversal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def levelOrder(self, root: TreeNode | None) -> list[list[int]]:
4949
self._levelOrder(root, 0, levels)
5050
return levels
5151

52-
def _levelOrder(self, root: TreeNode | None, level: int, levels: list[list[int]]) -> None:
52+
def _levelOrder(
53+
self, root: TreeNode | None, level: int, levels: list[list[int]]
54+
) -> None:
5355
if not root:
5456
return
5557
if len(levels) < level + 1:

src/classes/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class TreeNode(Generic[T]):
2222
A tree node in a binary tree.
2323
"""
2424

25-
def __init__(self, val: T, left: Self | None = None, right: Self | None = None) -> None:
25+
def __init__(
26+
self, val: T, left: Self | None = None, right: Self | None = None
27+
) -> None:
2628
self.val: T = val
2729
self.left: TreeNode[T] | None = right
2830
self.right: TreeNode[T] | None = left

src/flood_fill.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class Solution:
5858
Uses breadth-first search.
5959
"""
6060

61-
def floodFill(self, image: list[list[int]], sr: int, sc: int, color: int) -> list[list[int]]:
61+
def floodFill(
62+
self, image: list[list[int]], sr: int, sc: int, color: int
63+
) -> list[list[int]]:
6264
# If the starting pixel is already our target color, return `image`,
6365
# since no pixels will be modified.
6466
if image[sr][sc] == color:
@@ -104,7 +106,9 @@ class SolutionDFS:
104106
Uses depth-first search.
105107
"""
106108

107-
def floodFill(self, image: list[list[int]], sr: int, sc: int, color: int) -> list[list[int]]:
109+
def floodFill(
110+
self, image: list[list[int]], sr: int, sc: int, color: int
111+
) -> list[list[int]]:
108112
# If the starting pixel is already our target color, return `image`,
109113
# since no pixels will be modified.
110114
if image[sr][sc] == color:

src/lowest_common_ancestor_of_a_binary_search_tree.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929

3030
class Solution:
31-
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
31+
def lowestCommonAncestor(
32+
self, root: TreeNode, p: TreeNode, q: TreeNode
33+
) -> TreeNode:
3234
while True:
3335
if root.val > p.val and root.val > q.val and root.left:
3436
root = root.left

src/lowest_common_ancestor_of_a_binary_tree.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class Solution:
103103
Returns the LCA of `p` and `q` for the tree rooted at `root` recursively.
104104
"""
105105

106-
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
106+
def lowestCommonAncestor(
107+
self, root: TreeNode, p: TreeNode, q: TreeNode
108+
) -> TreeNode:
107109
# Base Case: If root is a leaf (None) or if root is p or q, return
108110
# root.
109111
if not root or root in (p, q):
@@ -135,7 +137,9 @@ class IterativeSolution:
135137
Returns the LCA of `p` and `q` for the tree rooted at `root` iteratively.
136138
"""
137139

138-
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
140+
def lowestCommonAncestor(
141+
self, root: TreeNode, p: TreeNode, q: TreeNode
142+
) -> TreeNode:
139143
stack: deque[TreeNode] = deque([root])
140144
parents: dict[TreeNode, TreeNode | None] = {root: None}
141145
curr: TreeNode = root

src/merge_two_sorted_lists.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757

5858

5959
class Solution:
60-
def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None:
60+
def mergeTwoLists(
61+
self, list1: ListNode | None, list2: ListNode | None
62+
) -> ListNode | None:
6163
prehead = ListNode(-1)
6264
curr = prehead
6365
# 1. Compare the heads of the two lists.

src/reorder_list.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ def reverseList(self, head: ListNode | None) -> ListNode | None:
109109
curr = _next
110110
return prev
111111

112-
def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None:
112+
def mergeTwoLists(
113+
self, list1: ListNode | None, list2: ListNode | None
114+
) -> ListNode | None:
113115
"""
114116
Given the head of two singly linked-lists, merge them starting with the
115117
head of the first list.

src/subtree_of_another_tree.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def isSameTree(self, t1: TreeNode | None, t2: TreeNode | None) -> bool:
3838
return False
3939

4040
if t1.val == t2.val:
41-
return self.isSameTree(t1.left, t2.left) and self.isSameTree(t1.right, t2.right)
41+
return self.isSameTree(t1.left, t2.left) and self.isSameTree(
42+
t1.right, t2.right
43+
)
4244

4345
return False

src/top_k_frequent_elements.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def topKFrequent(self, nums: list[int], k: int) -> list[int]:
131131
# is sorted anyway.
132132
return sorted(l[:k])
133133

134-
def quickselect(self, d: dict[int, int], l: list[int], left: int, right: int, k: int) -> int:
134+
def quickselect(
135+
self, d: dict[int, int], l: list[int], left: int, right: int, k: int
136+
) -> int:
135137
"""
136138
Return the kth element (0-based) in the given list.
137139
"""

src/valid_palindrome.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,21 @@ def isPalindrome(self, s: str) -> bool:
5252
p0, p1 = 0, len(s) - 1
5353
while p0 < p1:
5454
c0, c1 = ord(s[p0]), ord(s[p1])
55-
if (ASCII_0 <= c0 <= ASCII_9) or (ASCII_A <= c0 <= ASCII_Z) or (ASCII_a <= c0 <= ASCII_z):
55+
if (
56+
(ASCII_0 <= c0 <= ASCII_9)
57+
or (ASCII_A <= c0 <= ASCII_Z)
58+
or (ASCII_a <= c0 <= ASCII_z)
59+
):
5660
if ASCII_A <= c0 <= ASCII_Z:
5761
c0 = c0 + OFFSET
5862
else:
5963
p0 = p0 + 1
6064
continue
61-
if (ASCII_0 <= c1 <= ASCII_9) or (ASCII_A <= c1 <= ASCII_Z) or (ASCII_a <= c1 <= ASCII_z):
65+
if (
66+
(ASCII_0 <= c1 <= ASCII_9)
67+
or (ASCII_A <= c1 <= ASCII_Z)
68+
or (ASCII_a <= c1 <= ASCII_z)
69+
):
6270
if ASCII_A <= c1 <= ASCII_Z:
6371
c1 = c1 + OFFSET
6472
else:

src/validate_binary_search_tree.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ def _isValidBST(self, root: TreeNode, lo: int, hi: int) -> bool:
7272
# Recursively apply the minimal subtree solution to all subtrees. Any
7373
# invalid subtree will cause the bitwise AND operation to return False
7474
# (0) for the entire tree.
75-
return self._isValidBST(root.left, lo, root.val) and self._isValidBST(root.right, root.val, hi)
75+
return self._isValidBST(root.left, lo, root.val) and self._isValidBST(
76+
root.right, root.val, hi
77+
)

tests/test_best_time_to_buy_and_sell_stock.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
from unittest import TestCase
88

9-
from src.best_time_to_buy_and_sell_stock import NaiveSolution, OptimizedNaiveSolution, SlidingWindowSolution, Solution
9+
from src.best_time_to_buy_and_sell_stock import (
10+
NaiveSolution,
11+
OptimizedNaiveSolution,
12+
SlidingWindowSolution,
13+
Solution,
14+
)
1015

1116

1217
class TestSolution(TestCase):

tests/test_construct_binary_tree_from_preorder_and_inorder_traversal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
class TestSolution(TestCase):
1414
def test_1(self):
1515
exp = [3, 9, 20, None, None, 15, 7]
16-
assert_tree(Solution().buildTree(preorder=[3, 9, 20, 15, 7], inorder=[9, 3, 15, 20, 7]), exp)
16+
assert_tree(
17+
Solution().buildTree(preorder=[3, 9, 20, 15, 7], inorder=[9, 3, 15, 20, 7]),
18+
exp,
19+
)
1720

1821
def test_2(self):
1922
exp = [-1]

tests/test_flood_fill.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def test_2(self):
2222
class TestSolutionDFS(TestCase):
2323
def test_1(self):
2424
exp = [[2, 2, 2], [2, 2, 0], [2, 0, 1]]
25-
assert SolutionDFS().floodFill([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 2) == exp
25+
assert (
26+
SolutionDFS().floodFill([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 2) == exp
27+
)
2628

2729
def test_2(self):
2830
exp = [[0, 0, 0], [0, 0, 0]]

tests/test_longest_common_subsequence.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
from unittest import TestCase
88

9-
from src.longest_common_subsequence import AlternativeSolution, MemoizationSolution, RecursiveSolution, Solution
9+
from src.longest_common_subsequence import (
10+
AlternativeSolution,
11+
MemoizationSolution,
12+
RecursiveSolution,
13+
Solution,
14+
)
1015

1116

1217
class TestSolution(TestCase):
@@ -24,7 +29,10 @@ def test_3(self):
2429

2530
def test_4(self):
2631
exp = 4
27-
assert Solution().longestCommonSubsequence("pmjghexybyrgzczy", "hafcdqbgncrcbihkd") == exp
32+
assert (
33+
Solution().longestCommonSubsequence("pmjghexybyrgzczy", "hafcdqbgncrcbihkd")
34+
== exp
35+
)
2836

2937

3038
class TestAlternativeSolution(TestCase):
@@ -42,7 +50,12 @@ def test_3(self):
4250

4351
def test_4(self):
4452
exp = 4
45-
assert AlternativeSolution().longestCommonSubsequence("pmjghexybyrgzczy", "hafcdqbgncrcbihkd") == exp
53+
assert (
54+
AlternativeSolution().longestCommonSubsequence(
55+
"pmjghexybyrgzczy", "hafcdqbgncrcbihkd"
56+
)
57+
== exp
58+
)
4659

4760

4861
class TestMemoizationSolution(TestCase):
@@ -60,7 +73,12 @@ def test_3(self):
6073

6174
def test_4(self):
6275
exp = 4
63-
assert MemoizationSolution().longestCommonSubsequence("pmjghexybyrgzczy", "hafcdqbgncrcbihkd") == exp
76+
assert (
77+
MemoizationSolution().longestCommonSubsequence(
78+
"pmjghexybyrgzczy", "hafcdqbgncrcbihkd"
79+
)
80+
== exp
81+
)
6482

6583

6684
class TestRecursiveSolution(TestCase):
@@ -78,4 +96,9 @@ def test_3(self):
7896

7997
def test_4(self):
8098
exp = 4
81-
assert MemoizationSolution().longestCommonSubsequence("pmjghexybyrgzczy", "hafcdqbgncrcbihkd") == exp
99+
assert (
100+
MemoizationSolution().longestCommonSubsequence(
101+
"pmjghexybyrgzczy", "hafcdqbgncrcbihkd"
102+
)
103+
== exp
104+
)

tests/test_longest_increasing_subsequence.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
from unittest import TestCase
88

9-
from src.longest_increasing_subsequence import MemoizationSolution, RecursiveSolution, Solution
9+
from src.longest_increasing_subsequence import (
10+
MemoizationSolution,
11+
RecursiveSolution,
12+
Solution,
13+
)
1014

1115

1216
class TestSolution(TestCase):
@@ -50,7 +54,10 @@ def test_4(self):
5054

5155
def test_5(self):
5256
exp = 6
53-
assert MemoizationSolution().lengthOfLIS([3, 5, 6, 2, 5, 4, 19, 5, 6, 7, 12]) == exp
57+
assert (
58+
MemoizationSolution().lengthOfLIS([3, 5, 6, 2, 5, 4, 19, 5, 6, 7, 12])
59+
== exp
60+
)
5461

5562

5663
class TestRecursiveSolution(TestCase):
@@ -72,4 +79,6 @@ def test_4(self):
7279

7380
def test_5(self):
7481
exp = 6
75-
assert RecursiveSolution().lengthOfLIS([3, 5, 6, 2, 5, 4, 19, 5, 6, 7, 12]) == exp
82+
assert (
83+
RecursiveSolution().lengthOfLIS([3, 5, 6, 2, 5, 4, 19, 5, 6, 7, 12]) == exp
84+
)

tests/test_maximum_subarray.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def test_3(self):
2626
class TestTabularizationSolution(TestCase):
2727
def test_1(self):
2828
exp = 6
29-
assert TabularizationSolution().maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == exp
29+
assert (
30+
TabularizationSolution().maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]) == exp
31+
)
3032

3133
def test_2(self):
3234
exp = 1

tests/test_valid_palindrome.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def test_4(self):
3030
class TestSimplifiedSolution(TestCase):
3131
def test_1(self):
3232
exp = True
33-
assert SimplifiedSolution().isPalindrome("A man, a plan, a canal: Panama") == exp
33+
assert (
34+
SimplifiedSolution().isPalindrome("A man, a plan, a canal: Panama") == exp
35+
)
3436

3537
def test_2(self):
3638
exp = False

tests/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def _create_binary_tree_from_list(l: list[int | None], i: int) -> TreeNode | Non
131131
return node
132132

133133

134-
def create_bfs_list_from_binary_tree(*, root: TreeNode | None, values_only: bool = True) -> list[TreeNode | T]:
134+
def create_bfs_list_from_binary_tree(
135+
*, root: TreeNode | None, values_only: bool = True
136+
) -> list[TreeNode | T]:
135137
"""
136138
Creates a list of nodes in breadth-first search order given the root node
137139
of a binary tree. If values_only is False, the list contains the TreeNode

0 commit comments

Comments
 (0)