mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[pre-commit.ci] pre-commit autoupdate (#11473)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.4.10 → v0.5.0](https://github.com/astral-sh/ruff-pre-commit/compare/v0.4.10...v0.5.0) - [github.com/pre-commit/mirrors-mypy: v1.10.0 → v1.10.1](https://github.com/pre-commit/mirrors-mypy/compare/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 <cclauss@me.com>
This commit is contained in:
parent
6882a8b808
commit
716bdeb68b
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.4.10
|
rev: v0.5.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -47,10 +47,11 @@ repos:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v1.10.0
|
rev: v1.10.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
args:
|
args:
|
||||||
|
- --explicit-package-bases
|
||||||
- --ignore-missing-imports
|
- --ignore-missing-imports
|
||||||
- --install-types # See mirrors-mypy README.md
|
- --install-types # See mirrors-mypy README.md
|
||||||
- --non-interactive
|
- --non-interactive
|
||||||
|
|
|
@ -24,10 +24,10 @@ def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
|
||||||
]
|
]
|
||||||
permissible_positions = []
|
permissible_positions = []
|
||||||
|
|
||||||
for position in positions:
|
for inner_position in positions:
|
||||||
y_test, x_test = position
|
y_test, x_test = inner_position
|
||||||
if 0 <= y_test < n and 0 <= x_test < n:
|
if 0 <= y_test < n and 0 <= x_test < n:
|
||||||
permissible_positions.append(position)
|
permissible_positions.append(inner_position)
|
||||||
|
|
||||||
return permissible_positions
|
return permissible_positions
|
||||||
|
|
||||||
|
|
|
@ -80,9 +80,9 @@ class Node:
|
||||||
"""
|
"""
|
||||||
if self.left and (self.data < self.left.data or not self.left.is_sorted):
|
if self.left and (self.data < self.left.data or not self.left.is_sorted):
|
||||||
return False
|
return False
|
||||||
if self.right and (self.data > self.right.data or not self.right.is_sorted):
|
return not (
|
||||||
return False
|
self.right and (self.data > self.right.data or not self.right.is_sorted)
|
||||||
return True
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
"""
|
|
||||||
psf/black : true
|
|
||||||
ruff : passed
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
@ -321,9 +316,7 @@ class RedBlackTree:
|
||||||
return False
|
return False
|
||||||
if self.left and not self.left.check_coloring():
|
if self.left and not self.left.check_coloring():
|
||||||
return False
|
return False
|
||||||
if self.right and not self.right.check_coloring():
|
return not (self.right and not self.right.check_coloring())
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def black_height(self) -> int | None:
|
def black_height(self) -> int | None:
|
||||||
"""Returns the number of black nodes from this node to the
|
"""Returns the number of black nodes from this node to the
|
||||||
|
@ -561,9 +554,7 @@ def test_rotations() -> bool:
|
||||||
right_rot.right.right = RedBlackTree(10, parent=right_rot.right)
|
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.left = RedBlackTree(5, parent=right_rot.right.right)
|
||||||
right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
|
right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
|
||||||
if tree != right_rot:
|
return tree == right_rot
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def test_insertion_speed() -> bool:
|
def test_insertion_speed() -> bool:
|
||||||
|
@ -606,13 +597,11 @@ def test_insert_and_search() -> bool:
|
||||||
tree.insert(12)
|
tree.insert(12)
|
||||||
tree.insert(10)
|
tree.insert(10)
|
||||||
tree.insert(11)
|
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
|
# Found something not in there
|
||||||
return False
|
return False
|
||||||
if not (11 in tree and 12 in tree and -8 in tree and 0 in tree):
|
# Find all these things in there
|
||||||
# Didn't find something in there
|
return all(i in tree for i in (11, 12, -8, 0))
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def test_insert_delete() -> bool:
|
def test_insert_delete() -> bool:
|
||||||
|
@ -634,9 +623,7 @@ def test_insert_delete() -> bool:
|
||||||
tree = tree.remove(9)
|
tree = tree.remove(9)
|
||||||
if not tree.check_color_properties():
|
if not tree.check_color_properties():
|
||||||
return False
|
return False
|
||||||
if list(tree.inorder_traverse()) != [-8, 0, 4, 8, 10, 11, 12]:
|
return list(tree.inorder_traverse()) == [-8, 0, 4, 8, 10, 11, 12]
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def test_floor_ceil() -> bool:
|
def test_floor_ceil() -> bool:
|
||||||
|
@ -664,9 +651,7 @@ def test_min_max() -> bool:
|
||||||
tree.insert(24)
|
tree.insert(24)
|
||||||
tree.insert(20)
|
tree.insert(20)
|
||||||
tree.insert(22)
|
tree.insert(22)
|
||||||
if tree.get_max() != 22 or tree.get_min() != -16:
|
return not (tree.get_max() != 22 or tree.get_min() != -16)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def test_tree_traversal() -> bool:
|
def test_tree_traversal() -> bool:
|
||||||
|
@ -682,9 +667,7 @@ def test_tree_traversal() -> bool:
|
||||||
return False
|
return False
|
||||||
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
|
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
|
||||||
return False
|
return False
|
||||||
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
|
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def test_tree_chaining() -> bool:
|
def test_tree_chaining() -> bool:
|
||||||
|
@ -695,9 +678,7 @@ def test_tree_chaining() -> bool:
|
||||||
return False
|
return False
|
||||||
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
|
if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
|
||||||
return False
|
return False
|
||||||
if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]:
|
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def print_results(msg: str, passes: bool) -> None:
|
def print_results(msg: str, passes: bool) -> None:
|
||||||
|
|
0
docs/source/__init__.py
Normal file
0
docs/source/__init__.py
Normal file
|
@ -156,9 +156,11 @@ class GraphAdjacencyMatrix(Generic[T]):
|
||||||
self.vertex_to_index.pop(vertex)
|
self.vertex_to_index.pop(vertex)
|
||||||
|
|
||||||
# decrement indices for vertices shifted by the deleted vertex in the adj matrix
|
# decrement indices for vertices shifted by the deleted vertex in the adj matrix
|
||||||
for vertex in self.vertex_to_index:
|
for inner_vertex in self.vertex_to_index:
|
||||||
if self.vertex_to_index[vertex] >= start_index:
|
if self.vertex_to_index[inner_vertex] >= start_index:
|
||||||
self.vertex_to_index[vertex] = self.vertex_to_index[vertex] - 1
|
self.vertex_to_index[inner_vertex] = (
|
||||||
|
self.vertex_to_index[inner_vertex] - 1
|
||||||
|
)
|
||||||
|
|
||||||
def contains_vertex(self, vertex: T) -> bool:
|
def contains_vertex(self, vertex: T) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -123,9 +123,7 @@ def do_something(back_pointer, goal, start):
|
||||||
def valid(p: TPos):
|
def valid(p: TPos):
|
||||||
if p[0] < 0 or p[0] > n - 1:
|
if p[0] < 0 or p[0] > n - 1:
|
||||||
return False
|
return False
|
||||||
if p[1] < 0 or p[1] > n - 1:
|
return not (p[1] < 0 or p[1] > n - 1)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def expand_state(
|
def expand_state(
|
||||||
|
|
|
@ -103,4 +103,4 @@ if __name__ == "__main__":
|
||||||
edges = list(zip(source, target))
|
edges = list(zip(source, target))
|
||||||
g = create_graph(n_vertices, edges)
|
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]]
|
||||||
|
|
|
@ -82,8 +82,8 @@ def reformat_hex(i: int) -> bytes:
|
||||||
|
|
||||||
hex_rep = format(i, "08x")[-8:]
|
hex_rep = format(i, "08x")[-8:]
|
||||||
little_endian_hex = b""
|
little_endian_hex = b""
|
||||||
for i in [3, 2, 1, 0]:
|
for j in [3, 2, 1, 0]:
|
||||||
little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8")
|
little_endian_hex += hex_rep[2 * j : 2 * j + 2].encode("utf-8")
|
||||||
return little_endian_hex
|
return little_endian_hex
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -84,7 +84,6 @@ class FFT:
|
||||||
# Corner case
|
# Corner case
|
||||||
if len(dft) <= 1:
|
if len(dft) <= 1:
|
||||||
return dft[0]
|
return dft[0]
|
||||||
#
|
|
||||||
next_ncol = self.c_max_length // 2
|
next_ncol = self.c_max_length // 2
|
||||||
while next_ncol > 0:
|
while next_ncol > 0:
|
||||||
new_dft = [[] for i in range(next_ncol)]
|
new_dft = [[] for i in range(next_ncol)]
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -85,11 +85,10 @@ def validate(n: int) -> bool:
|
||||||
>>> validate(3797)
|
>>> validate(3797)
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if len(str(n)) > 3 and (
|
return not (
|
||||||
not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3]))
|
len(str(n)) > 3
|
||||||
):
|
and (not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])))
|
||||||
return False
|
)
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def compute_truncated_primes(count: int = 11) -> list[int]:
|
def compute_truncated_primes(count: int = 11) -> list[int]:
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -43,7 +43,7 @@ def solution(limit: int = 1_000_000) -> int:
|
||||||
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
|
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
|
||||||
phi[ind] -= phi[ind] // i
|
phi[ind] -= phi[ind] // i
|
||||||
|
|
||||||
return np.sum(phi[2 : limit + 1])
|
return int(np.sum(phi[2 : limit + 1]))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -1 +0,0 @@
|
||||||
#
|
|
|
@ -36,7 +36,7 @@ def build_tree() -> TreeNode:
|
||||||
right_node = TreeNode(int(check))
|
right_node = TreeNode(int(check))
|
||||||
node_found.right = right_node
|
node_found.right = right_node
|
||||||
q.put(right_node)
|
q.put(right_node)
|
||||||
raise
|
raise ValueError("Something went wrong")
|
||||||
|
|
||||||
|
|
||||||
def pre_order(node: TreeNode) -> None:
|
def pre_order(node: TreeNode) -> None:
|
||||||
|
@ -164,8 +164,8 @@ def level_order_actual(node: TreeNode) -> None:
|
||||||
if node_dequeued.right:
|
if node_dequeued.right:
|
||||||
list_.append(node_dequeued.right)
|
list_.append(node_dequeued.right)
|
||||||
print()
|
print()
|
||||||
for node in list_:
|
for inner_node in list_:
|
||||||
q.put(node)
|
q.put(inner_node)
|
||||||
|
|
||||||
|
|
||||||
# iteration version
|
# iteration version
|
||||||
|
|
|
@ -77,10 +77,7 @@ class FilesArray:
|
||||||
self.empty.add(i)
|
self.empty.add(i)
|
||||||
self.files[i].close()
|
self.files[i].close()
|
||||||
|
|
||||||
if len(self.empty) == self.num_buffers:
|
return len(self.empty) != self.num_buffers
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def unshift(self, index):
|
def unshift(self, index):
|
||||||
value = self.buffers[index]
|
value = self.buffers[index]
|
||||||
|
|
0
source/__init__.py
Normal file
0
source/__init__.py
Normal file
|
@ -72,9 +72,7 @@ def can_string_be_rearranged_as_palindrome(input_str: str = "") -> bool:
|
||||||
for character_count in character_freq_dict.values():
|
for character_count in character_freq_dict.values():
|
||||||
if character_count % 2:
|
if character_count % 2:
|
||||||
odd_char += 1
|
odd_char += 1
|
||||||
if odd_char > 1:
|
return not odd_char > 1
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def benchmark(input_str: str = "") -> None:
|
def benchmark(input_str: str = "") -> None:
|
||||||
|
|
|
@ -101,9 +101,7 @@ def is_valid_email_address(email: str) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# (7.) Validate the placement of "." characters
|
# (7.) Validate the placement of "." characters
|
||||||
if domain.startswith(".") or domain.endswith(".") or ".." in domain:
|
return not (domain.startswith(".") or domain.endswith(".") or ".." in domain)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -67,19 +67,19 @@ def text_justification(word: str, max_width: int) -> list:
|
||||||
answer = []
|
answer = []
|
||||||
line: list[str] = []
|
line: list[str] = []
|
||||||
width = 0
|
width = 0
|
||||||
for word in words:
|
for inner_word in words:
|
||||||
if width + len(word) + len(line) <= max_width:
|
if width + len(inner_word) + len(line) <= max_width:
|
||||||
# keep adding words until we can fill out max_width
|
# keep adding words until we can fill out max_width
|
||||||
# width = sum of length of all words (without overall_spaces_count)
|
# 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
|
# len(line) = number of overall_spaces_count to insert between words
|
||||||
line.append(word)
|
line.append(inner_word)
|
||||||
width += len(word)
|
width += len(inner_word)
|
||||||
else:
|
else:
|
||||||
# justify the line and add it to result
|
# justify the line and add it to result
|
||||||
answer.append(justify(line, width, max_width))
|
answer.append(justify(line, width, max_width))
|
||||||
# reset new line and new 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)
|
remaining_spaces = max_width - width - len(line)
|
||||||
answer.append(" ".join(line) + (remaining_spaces + 1) * " ")
|
answer.append(" ".join(line) + (remaining_spaces + 1) * " ")
|
||||||
return answer
|
return answer
|
||||||
|
|
Loading…
Reference in New Issue
Block a user