Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pre-commit.ci] pre-commit autoupdate #11473

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
- id: auto-walrus

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.10
rev: v0.5.0
hooks:
- id: ruff
- id: ruff-format
Expand Down Expand Up @@ -47,10 +47,11 @@ repos:
- id: validate-pyproject

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
rev: v1.10.1
hooks:
- id: mypy
args:
- --explicit-package-bases
- --ignore-missing-imports
- --install-types # See mirrors-mypy README.md
- --non-interactive
Expand Down
6 changes: 3 additions & 3 deletions backtracking/knight_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
]
permissible_positions = []

for position in positions:
y_test, x_test = position
for inner_position in positions:
y_test, x_test = inner_position
if 0 <= y_test < n and 0 <= x_test < n:
permissible_positions.append(position)
permissible_positions.append(inner_position)

return permissible_positions

Expand Down
6 changes: 3 additions & 3 deletions data_structures/binary_tree/is_sorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def is_sorted(self) -> bool:
"""
if self.left and (self.data < self.left.data or not self.left.is_sorted):
return False
if self.right and (self.data > self.right.data or not self.right.is_sorted):
return False
return True
return not (
self.right and (self.data > self.right.data or not self.right.is_sorted)
)


if __name__ == "__main__":
Expand Down
37 changes: 9 additions & 28 deletions data_structures/binary_tree/red_black_tree.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
"""
psf/black : true
ruff : passed
"""

from __future__ import annotations

from collections.abc import Iterator
Expand Down Expand Up @@ -321,9 +316,7 @@ def check_coloring(self) -> bool:
return False
if self.left and not self.left.check_coloring():
return False
if self.right and not self.right.check_coloring():
return False
return True
return not (self.right and not self.right.check_coloring())

def black_height(self) -> int | None:
"""Returns the number of black nodes from this node to the
Expand Down Expand Up @@ -561,9 +554,7 @@ def test_rotations() -> bool:
right_rot.right.right = RedBlackTree(10, parent=right_rot.right)
right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right)
right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
if tree != right_rot:
return False
return True
return tree == right_rot


def test_insertion_speed() -> bool:
Expand Down Expand Up @@ -606,13 +597,11 @@ def test_insert_and_search() -> bool:
tree.insert(12)
tree.insert(10)
tree.insert(11)
if 5 in tree or -6 in tree or -10 in tree or 13 in tree:
if any(i in tree for i in (5, -6, -10, 13)):
# Found something not in there
return False
if not (11 in tree and 12 in tree and -8 in tree and 0 in tree):
# Didn't find something in there
return False
return True
# Find all these things in there
return all(i in tree for i in (11, 12, -8, 0))


def test_insert_delete() -> bool:
Expand All @@ -634,9 +623,7 @@ def test_insert_delete() -> bool:
tree = tree.remove(9)
if not tree.check_color_properties():
return False
if list(tree.inorder_traverse()) != [-8, 0, 4, 8, 10, 11, 12]:
return False
return True
return list(tree.inorder_traverse()) == [-8, 0, 4, 8, 10, 11, 12]


def test_floor_ceil() -> bool:
Expand Down Expand Up @@ -664,9 +651,7 @@ def test_min_max() -> bool:
tree.insert(24)
tree.insert(20)
tree.insert(22)
if tree.get_max() != 22 or tree.get_min() != -16:
return False
return True
return not (tree.get_max() != 22 or tree.get_min() != -16)


def test_tree_traversal() -> bool:
Expand All @@ -682,9 +667,7 @@ def test_tree_traversal() -> bool:
return False
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
return False
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
return False
return True
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]


def test_tree_chaining() -> bool:
Expand All @@ -695,9 +678,7 @@ def test_tree_chaining() -> bool:
return False
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
return False
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
return False
return True
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]


def print_results(msg: str, passes: bool) -> None:
Expand Down
Empty file added docs/source/__init__.py
Empty file.
8 changes: 5 additions & 3 deletions graphs/graph_adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ def remove_vertex(self, vertex: T) -> None:
self.vertex_to_index.pop(vertex)

# decrement indices for vertices shifted by the deleted vertex in the adj matrix
for vertex in self.vertex_to_index:
if self.vertex_to_index[vertex] >= start_index:
self.vertex_to_index[vertex] = self.vertex_to_index[vertex] - 1
for inner_vertex in self.vertex_to_index:
if self.vertex_to_index[inner_vertex] >= start_index:
self.vertex_to_index[inner_vertex] = (
self.vertex_to_index[inner_vertex] - 1
)

def contains_vertex(self, vertex: T) -> bool:
"""
Expand Down
4 changes: 1 addition & 3 deletions graphs/multi_heuristic_astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def do_something(back_pointer, goal, start):
def valid(p: TPos):
if p[0] < 0 or p[0] > n - 1:
return False
if p[1] < 0 or p[1] > n - 1:
return False
return True
return not (p[1] < 0 or p[1] > n - 1)


def expand_state(
Expand Down
2 changes: 1 addition & 1 deletion graphs/tarjans_scc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def create_graph(n: int, edges: list[tuple[int, int]]) -> list[list[int]]:
edges = list(zip(source, target))
g = create_graph(n_vertices, edges)

assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
assert tarjan(g) == [[5], [6], [4], [3, 2, 1, 0]]
4 changes: 2 additions & 2 deletions hashes/md5.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def reformat_hex(i: int) -> bytes:

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


Expand Down
1 change: 0 additions & 1 deletion maths/radix2_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def __dft(self, which):
# Corner case
if len(dft) <= 1:
return dft[0]
#
next_ncol = self.c_max_length // 2
while next_ncol > 0:
new_dft = [[] for i in range(next_ncol)]
Expand Down
1 change: 0 additions & 1 deletion project_euler/problem_034/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_035/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_037/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
9 changes: 4 additions & 5 deletions project_euler/problem_037/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ def validate(n: int) -> bool:
>>> validate(3797)
True
"""
if len(str(n)) > 3 and (
not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3]))
):
return False
return True
return not (
len(str(n)) > 3
and (not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])))
)


def compute_truncated_primes(count: int = 11) -> list[int]:
Expand Down
1 change: 0 additions & 1 deletion project_euler/problem_039/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_041/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_043/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_044/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_045/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_046/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_055/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_058/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_063/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
2 changes: 1 addition & 1 deletion project_euler/problem_072/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def solution(limit: int = 1_000_000) -> int:
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
phi[ind] -= phi[ind] // i

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


if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion project_euler/problem_089/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
1 change: 0 additions & 1 deletion project_euler/problem_097/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
6 changes: 3 additions & 3 deletions searches/binary_tree_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def build_tree() -> TreeNode:
right_node = TreeNode(int(check))
node_found.right = right_node
q.put(right_node)
raise
raise ValueError("Something went wrong")


def pre_order(node: TreeNode) -> None:
Expand Down Expand Up @@ -164,8 +164,8 @@ def level_order_actual(node: TreeNode) -> None:
if node_dequeued.right:
list_.append(node_dequeued.right)
print()
for node in list_:
q.put(node)
for inner_node in list_:
q.put(inner_node)


# iteration version
Expand Down
5 changes: 1 addition & 4 deletions sorts/external_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ def refresh(self):
self.empty.add(i)
self.files[i].close()

if len(self.empty) == self.num_buffers:
return False

return True
return len(self.empty) != self.num_buffers

def unshift(self, index):
value = self.buffers[index]
Expand Down
Empty file added source/__init__.py
Empty file.
4 changes: 1 addition & 3 deletions strings/can_string_be_rearranged_as_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def can_string_be_rearranged_as_palindrome(input_str: str = "") -> bool:
for character_count in character_freq_dict.values():
if character_count % 2:
odd_char += 1
if odd_char > 1:
return False
return True
return not odd_char > 1


def benchmark(input_str: str = "") -> None:
Expand Down
4 changes: 1 addition & 3 deletions strings/is_valid_email_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def is_valid_email_address(email: str) -> bool:
return False

# (7.) Validate the placement of "." characters
if domain.startswith(".") or domain.endswith(".") or ".." in domain:
return False
return True
return not (domain.startswith(".") or domain.endswith(".") or ".." in domain)


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions strings/text_justification.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ def justify(line: list, width: int, max_width: int) -> str:
answer = []
line: list[str] = []
width = 0
for word in words:
if width + len(word) + len(line) <= max_width:
for inner_word in words:
if width + len(inner_word) + len(line) <= max_width:
# keep adding words until we can fill out max_width
# width = sum of length of all words (without overall_spaces_count)
# len(word) = length of current word
# len(inner_word) = length of current inner_word
# len(line) = number of overall_spaces_count to insert between words
line.append(word)
width += len(word)
line.append(inner_word)
width += len(inner_word)
else:
# justify the line and add it to result
answer.append(justify(line, width, max_width))
# reset new line and new width
line, width = [word], len(word)
line, width = [inner_word], len(inner_word)
remaining_spaces = max_width - width - len(line)
answer.append(" ".join(line) + (remaining_spaces + 1) * " ")
return answer
Expand Down