Skip to content

Commit 716bdeb

Browse files
[pre-commit.ci] pre-commit autoupdate (TheAlgorithms#11473)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.4.10 → v0.5.0](astral-sh/ruff-pre-commit@v0.4.10...v0.5.0) - [github.com/pre-commit/mirrors-mypy: v1.10.0 → v1.10.1](pre-commit/mirrors-mypy@v1.10.0...v1.10.1) * Fix ruff issues * Fix ruff issues --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 6882a8b commit 716bdeb

File tree

32 files changed

+44
-85
lines changed

32 files changed

+44
-85
lines changed

.pre-commit-config.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.4.10
19+
rev: v0.5.0
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format
@@ -47,10 +47,11 @@ repos:
4747
- id: validate-pyproject
4848

4949
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: v1.10.0
50+
rev: v1.10.1
5151
hooks:
5252
- id: mypy
5353
args:
54+
- --explicit-package-bases
5455
- --ignore-missing-imports
5556
- --install-types # See mirrors-mypy README.md
5657
- --non-interactive

backtracking/knight_tour.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
2424
]
2525
permissible_positions = []
2626

27-
for position in positions:
28-
y_test, x_test = position
27+
for inner_position in positions:
28+
y_test, x_test = inner_position
2929
if 0 <= y_test < n and 0 <= x_test < n:
30-
permissible_positions.append(position)
30+
permissible_positions.append(inner_position)
3131

3232
return permissible_positions
3333

data_structures/binary_tree/is_sorted.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def is_sorted(self) -> bool:
8080
"""
8181
if self.left and (self.data < self.left.data or not self.left.is_sorted):
8282
return False
83-
if self.right and (self.data > self.right.data or not self.right.is_sorted):
84-
return False
85-
return True
83+
return not (
84+
self.right and (self.data > self.right.data or not self.right.is_sorted)
85+
)
8686

8787

8888
if __name__ == "__main__":

data_structures/binary_tree/red_black_tree.py

+9-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
"""
2-
psf/black : true
3-
ruff : passed
4-
"""
5-
61
from __future__ import annotations
72

83
from collections.abc import Iterator
@@ -321,9 +316,7 @@ def check_coloring(self) -> bool:
321316
return False
322317
if self.left and not self.left.check_coloring():
323318
return False
324-
if self.right and not self.right.check_coloring():
325-
return False
326-
return True
319+
return not (self.right and not self.right.check_coloring())
327320

328321
def black_height(self) -> int | None:
329322
"""Returns the number of black nodes from this node to the
@@ -561,9 +554,7 @@ def test_rotations() -> bool:
561554
right_rot.right.right = RedBlackTree(10, parent=right_rot.right)
562555
right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right)
563556
right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
564-
if tree != right_rot:
565-
return False
566-
return True
557+
return tree == right_rot
567558

568559

569560
def test_insertion_speed() -> bool:
@@ -606,13 +597,11 @@ def test_insert_and_search() -> bool:
606597
tree.insert(12)
607598
tree.insert(10)
608599
tree.insert(11)
609-
if 5 in tree or -6 in tree or -10 in tree or 13 in tree:
600+
if any(i in tree for i in (5, -6, -10, 13)):
610601
# Found something not in there
611602
return False
612-
if not (11 in tree and 12 in tree and -8 in tree and 0 in tree):
613-
# Didn't find something in there
614-
return False
615-
return True
603+
# Find all these things in there
604+
return all(i in tree for i in (11, 12, -8, 0))
616605

617606

618607
def test_insert_delete() -> bool:
@@ -634,9 +623,7 @@ def test_insert_delete() -> bool:
634623
tree = tree.remove(9)
635624
if not tree.check_color_properties():
636625
return False
637-
if list(tree.inorder_traverse()) != [-8, 0, 4, 8, 10, 11, 12]:
638-
return False
639-
return True
626+
return list(tree.inorder_traverse()) == [-8, 0, 4, 8, 10, 11, 12]
640627

641628

642629
def test_floor_ceil() -> bool:
@@ -664,9 +651,7 @@ def test_min_max() -> bool:
664651
tree.insert(24)
665652
tree.insert(20)
666653
tree.insert(22)
667-
if tree.get_max() != 22 or tree.get_min() != -16:
668-
return False
669-
return True
654+
return not (tree.get_max() != 22 or tree.get_min() != -16)
670655

671656

672657
def test_tree_traversal() -> bool:
@@ -682,9 +667,7 @@ def test_tree_traversal() -> bool:
682667
return False
683668
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
684669
return False
685-
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
686-
return False
687-
return True
670+
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
688671

689672

690673
def test_tree_chaining() -> bool:
@@ -695,9 +678,7 @@ def test_tree_chaining() -> bool:
695678
return False
696679
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
697680
return False
698-
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
699-
return False
700-
return True
681+
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
701682

702683

703684
def print_results(msg: str, passes: bool) -> None:

docs/source/__init__.py

Whitespace-only changes.

graphs/graph_adjacency_matrix.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ def remove_vertex(self, vertex: T) -> None:
156156
self.vertex_to_index.pop(vertex)
157157

158158
# decrement indices for vertices shifted by the deleted vertex in the adj matrix
159-
for vertex in self.vertex_to_index:
160-
if self.vertex_to_index[vertex] >= start_index:
161-
self.vertex_to_index[vertex] = self.vertex_to_index[vertex] - 1
159+
for inner_vertex in self.vertex_to_index:
160+
if self.vertex_to_index[inner_vertex] >= start_index:
161+
self.vertex_to_index[inner_vertex] = (
162+
self.vertex_to_index[inner_vertex] - 1
163+
)
162164

163165
def contains_vertex(self, vertex: T) -> bool:
164166
"""

graphs/multi_heuristic_astar.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ def do_something(back_pointer, goal, start):
123123
def valid(p: TPos):
124124
if p[0] < 0 or p[0] > n - 1:
125125
return False
126-
if p[1] < 0 or p[1] > n - 1:
127-
return False
128-
return True
126+
return not (p[1] < 0 or p[1] > n - 1)
129127

130128

131129
def expand_state(

graphs/tarjans_scc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ def create_graph(n: int, edges: list[tuple[int, int]]) -> list[list[int]]:
103103
edges = list(zip(source, target))
104104
g = create_graph(n_vertices, edges)
105105

106-
assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
106+
assert tarjan(g) == [[5], [6], [4], [3, 2, 1, 0]]

hashes/md5.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def reformat_hex(i: int) -> bytes:
8282

8383
hex_rep = format(i, "08x")[-8:]
8484
little_endian_hex = b""
85-
for i in [3, 2, 1, 0]:
86-
little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8")
85+
for j in [3, 2, 1, 0]:
86+
little_endian_hex += hex_rep[2 * j : 2 * j + 2].encode("utf-8")
8787
return little_endian_hex
8888

8989

maths/radix2_fft.py

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def __dft(self, which):
8484
# Corner case
8585
if len(dft) <= 1:
8686
return dft[0]
87-
#
8887
next_ncol = self.c_max_length // 2
8988
while next_ncol > 0:
9089
new_dft = [[] for i in range(next_ncol)]

project_euler/problem_034/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_035/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_037/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_037/sol1.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ def validate(n: int) -> bool:
8585
>>> validate(3797)
8686
True
8787
"""
88-
if len(str(n)) > 3 and (
89-
not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3]))
90-
):
91-
return False
92-
return True
88+
return not (
89+
len(str(n)) > 3
90+
and (not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])))
91+
)
9392

9493

9594
def compute_truncated_primes(count: int = 11) -> list[int]:

project_euler/problem_039/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_041/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_043/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_044/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_045/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_046/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_055/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_058/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_063/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_072/sol1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def solution(limit: int = 1_000_000) -> int:
4343
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
4444
phi[ind] -= phi[ind] // i
4545

46-
return np.sum(phi[2 : limit + 1])
46+
return int(np.sum(phi[2 : limit + 1]))
4747

4848

4949
if __name__ == "__main__":

project_euler/problem_089/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

project_euler/problem_097/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#

searches/binary_tree_traversal.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def build_tree() -> TreeNode:
3636
right_node = TreeNode(int(check))
3737
node_found.right = right_node
3838
q.put(right_node)
39-
raise
39+
raise ValueError("Something went wrong")
4040

4141

4242
def pre_order(node: TreeNode) -> None:
@@ -164,8 +164,8 @@ def level_order_actual(node: TreeNode) -> None:
164164
if node_dequeued.right:
165165
list_.append(node_dequeued.right)
166166
print()
167-
for node in list_:
168-
q.put(node)
167+
for inner_node in list_:
168+
q.put(inner_node)
169169

170170

171171
# iteration version

sorts/external_sort.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ def refresh(self):
7777
self.empty.add(i)
7878
self.files[i].close()
7979

80-
if len(self.empty) == self.num_buffers:
81-
return False
82-
83-
return True
80+
return len(self.empty) != self.num_buffers
8481

8582
def unshift(self, index):
8683
value = self.buffers[index]

source/__init__.py

Whitespace-only changes.

strings/can_string_be_rearranged_as_palindrome.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def can_string_be_rearranged_as_palindrome(input_str: str = "") -> bool:
7272
for character_count in character_freq_dict.values():
7373
if character_count % 2:
7474
odd_char += 1
75-
if odd_char > 1:
76-
return False
77-
return True
75+
return not odd_char > 1
7876

7977

8078
def benchmark(input_str: str = "") -> None:

strings/is_valid_email_address.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def is_valid_email_address(email: str) -> bool:
101101
return False
102102

103103
# (7.) Validate the placement of "." characters
104-
if domain.startswith(".") or domain.endswith(".") or ".." in domain:
105-
return False
106-
return True
104+
return not (domain.startswith(".") or domain.endswith(".") or ".." in domain)
107105

108106

109107
if __name__ == "__main__":

strings/text_justification.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ def justify(line: list, width: int, max_width: int) -> str:
6767
answer = []
6868
line: list[str] = []
6969
width = 0
70-
for word in words:
71-
if width + len(word) + len(line) <= max_width:
70+
for inner_word in words:
71+
if width + len(inner_word) + len(line) <= max_width:
7272
# keep adding words until we can fill out max_width
7373
# width = sum of length of all words (without overall_spaces_count)
74-
# len(word) = length of current word
74+
# len(inner_word) = length of current inner_word
7575
# len(line) = number of overall_spaces_count to insert between words
76-
line.append(word)
77-
width += len(word)
76+
line.append(inner_word)
77+
width += len(inner_word)
7878
else:
7979
# justify the line and add it to result
8080
answer.append(justify(line, width, max_width))
8181
# reset new line and new width
82-
line, width = [word], len(word)
82+
line, width = [inner_word], len(inner_word)
8383
remaining_spaces = max_width - width - len(line)
8484
answer.append(" ".join(line) + (remaining_spaces + 1) * " ")
8585
return answer

0 commit comments

Comments
 (0)