Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit autoupdate
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
pre-commit-ci[bot] authored and cclauss committed Nov 25, 2024
1 parent f3f32ae commit 6c61cf0
Show file tree
Hide file tree
Showing 30 changed files with 66 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .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.7.4
rev: v0.8.0
hooks:
- id: ruff
- id: ruff-format
Expand Down
6 changes: 2 additions & 4 deletions cellular_automata/conways_game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ def new_generation(cells: list[list[int]]) -> list[list[int]]:
# 3. All other live cells die in the next generation.
# Similarly, all other dead cells stay dead.
alive = cells[i][j] == 1
if (
(alive and 2 <= neighbour_count <= 3)
or not alive
and neighbour_count == 3
if (alive and 2 <= neighbour_count <= 3) or (
not alive and neighbour_count == 3
):
next_generation_row.append(1)
else:
Expand Down
2 changes: 1 addition & 1 deletion ciphers/playfair_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from collections.abc import Generator, Iterable


def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...], None, None]:
def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...]]:
it = iter(seq)
while True:
chunk = tuple(itertools.islice(it, size))
Expand Down
2 changes: 1 addition & 1 deletion ciphers/simple_keyword_cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def remove_duplicates(key: str) -> str:

key_no_dups = ""
for ch in key:
if ch == " " or ch not in key_no_dups and ch.isalpha():
if ch == " " or (ch not in key_no_dups and ch.isalpha()):
key_no_dups += ch
return key_no_dups

Expand Down
6 changes: 2 additions & 4 deletions ciphers/transposition_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ def decrypt_message(key: int, message: str) -> str:
plain_text[col] += symbol
col += 1

if (
(col == num_cols)
or (col == num_cols - 1)
and (row >= num_rows - num_shaded_boxes)
if (col == num_cols) or (
(col == num_cols - 1) and (row >= num_rows - num_shaded_boxes)
):
col = 0
row += 1
Expand Down
4 changes: 2 additions & 2 deletions compression/lempel_ziv.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def add_key_to_lexicon(
lexicon[curr_string + "0"] = last_match_id

if math.log2(index).is_integer():
for curr_key in lexicon:
lexicon[curr_key] = "0" + lexicon[curr_key]
for curr_key, value in lexicon.items():
lexicon[curr_key] = f"0{value}"

lexicon[curr_string + "1"] = bin(index)[2:]

Expand Down
2 changes: 1 addition & 1 deletion data_structures/arrays/sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def time_solve(grid):
times, results = zip(*[time_solve(grid) for grid in grids])
if (n := len(grids)) > 1:
print(
"Solved %d of %d %s puzzles (avg %.2f secs (%d Hz), max %.2f secs)."
"Solved %d of %d %s puzzles (avg %.2f secs (%d Hz), max %.2f secs)." # noqa: UP031
% (sum(results), n, name, sum(times) / n, n / sum(times), max(times))
)

Expand Down
24 changes: 10 additions & 14 deletions data_structures/binary_tree/binary_tree_traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def make_tree() -> Node | None:
return tree


def preorder(root: Node | None) -> Generator[int, None, None]:
def preorder(root: Node | None) -> Generator[int]:
"""
Pre-order traversal visits root node, left subtree, right subtree.
>>> list(preorder(make_tree()))
Expand All @@ -43,7 +43,7 @@ def preorder(root: Node | None) -> Generator[int, None, None]:
yield from preorder(root.right)


def postorder(root: Node | None) -> Generator[int, None, None]:
def postorder(root: Node | None) -> Generator[int]:
"""
Post-order traversal visits left subtree, right subtree, root node.
>>> list(postorder(make_tree()))
Expand All @@ -56,7 +56,7 @@ def postorder(root: Node | None) -> Generator[int, None, None]:
yield root.data


def inorder(root: Node | None) -> Generator[int, None, None]:
def inorder(root: Node | None) -> Generator[int]:
"""
In-order traversal visits left subtree, root node, right subtree.
>>> list(inorder(make_tree()))
Expand All @@ -69,7 +69,7 @@ def inorder(root: Node | None) -> Generator[int, None, None]:
yield from inorder(root.right)


def reverse_inorder(root: Node | None) -> Generator[int, None, None]:
def reverse_inorder(root: Node | None) -> Generator[int]:
"""
Reverse in-order traversal visits right subtree, root node, left subtree.
>>> list(reverse_inorder(make_tree()))
Expand All @@ -93,7 +93,7 @@ def height(root: Node | None) -> int:
return (max(height(root.left), height(root.right)) + 1) if root else 0


def level_order(root: Node | None) -> Generator[int, None, None]:
def level_order(root: Node | None) -> Generator[int]:
"""
Returns a list of nodes value from a whole binary tree in Level Order Traverse.
Level Order traverse: Visit nodes of the tree level-by-level.
Expand All @@ -116,9 +116,7 @@ def level_order(root: Node | None) -> Generator[int, None, None]:
process_queue.append(node.right)


def get_nodes_from_left_to_right(
root: Node | None, level: int
) -> Generator[int, None, None]:
def get_nodes_from_left_to_right(root: Node | None, level: int) -> Generator[int]:
"""
Returns a list of nodes value from a particular level:
Left to right direction of the binary tree.
Expand All @@ -128,7 +126,7 @@ def get_nodes_from_left_to_right(
[2, 3]
"""

def populate_output(root: Node | None, level: int) -> Generator[int, None, None]:
def populate_output(root: Node | None, level: int) -> Generator[int]:
if not root:
return
if level == 1:
Expand All @@ -140,9 +138,7 @@ def populate_output(root: Node | None, level: int) -> Generator[int, None, None]
yield from populate_output(root, level)


def get_nodes_from_right_to_left(
root: Node | None, level: int
) -> Generator[int, None, None]:
def get_nodes_from_right_to_left(root: Node | None, level: int) -> Generator[int]:
"""
Returns a list of nodes value from a particular level:
Right to left direction of the binary tree.
Expand All @@ -152,7 +148,7 @@ def get_nodes_from_right_to_left(
[3, 2]
"""

def populate_output(root: Node | None, level: int) -> Generator[int, None, None]:
def populate_output(root: Node | None, level: int) -> Generator[int]:
if not root:
return
if level == 1:
Expand All @@ -164,7 +160,7 @@ def populate_output(root: Node | None, level: int) -> Generator[int, None, None]
yield from populate_output(root, level)


def zigzag(root: Node | None) -> Generator[int, None, None]:
def zigzag(root: Node | None) -> Generator[int]:
"""
ZigZag traverse:
Returns a list of nodes value from left to right and right to left, alternatively.
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list/deque_doubly.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _DoublyLinkedBase:
"""A Private class (to be inherited)"""

class _Node:
__slots__ = "_prev", "_data", "_next"
__slots__ = "_data", "_next", "_prev"

def __init__(self, link_p, element, link_n):
self._prev = link_p
Expand Down
2 changes: 1 addition & 1 deletion data_structures/queue/double_ended_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Deque:
the number of nodes
"""

__slots__ = ("_front", "_back", "_len")
__slots__ = ("_back", "_front", "_len")

@dataclass
class _Node:
Expand Down
Empty file added docs/source/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion electronics/electrical_impedance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations

from math import pow, sqrt
from math import pow, sqrt # noqa: A004


def electrical_impedance(
Expand Down
6 changes: 2 additions & 4 deletions graphs/ant_colony_optimization_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ def city_select(
IndexError: list index out of range
"""
probabilities = []
for city in unvisited_cities:
city_distance = distance(
unvisited_cities[city], next(iter(current_city.values()))
)
for city, value in unvisited_cities.items():
city_distance = distance(value, next(iter(current_city.values())))
probability = (pheromone[city][next(iter(current_city.keys()))] ** alpha) * (
(1 / city_distance) ** beta
)
Expand Down
22 changes: 11 additions & 11 deletions graphs/basic_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ def dijk(g, s):
if len(known) == len(g) - 1:
break
mini = 100000
for i in dist:
if i not in known and dist[i] < mini:
mini = dist[i]
u = i
for key, value in dist:
if key not in known and value < mini:
mini = value
u = key
known.add(u)
for v in g[u]:
if v[0] not in known and dist[u] + v[1] < dist.get(v[0], 100000):
dist[v[0]] = dist[u] + v[1]
path[v[0]] = u
for i in dist:
if i != s:
print(dist[i])
for key, value in dist.items():
if key != s:
print(value)


"""
Expand Down Expand Up @@ -255,10 +255,10 @@ def prim(g, s):
if len(known) == len(g) - 1:
break
mini = 100000
for i in dist:
if i not in known and dist[i] < mini:
mini = dist[i]
u = i
for key, value in dist.items():
if key not in known and value < mini:
mini = value
u = key
known.add(u)
for v in g[u]:
if v[0] not in known and v[1] < dist.get(v[0], 100000):
Expand Down
8 changes: 4 additions & 4 deletions graphs/minimum_spanning_tree_boruvka.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ def boruvka_mst(graph):

if cheap_edge[set2] == -1 or cheap_edge[set2][2] > weight:
cheap_edge[set2] = [head, tail, weight]
for vertex in cheap_edge:
if cheap_edge[vertex] != -1:
head, tail, weight = cheap_edge[vertex]
for head_tail_weight in cheap_edge.values():
if head_tail_weight != -1:
head, tail, weight = head_tail_weight
if union_find.find(head) != union_find.find(tail):
union_find.union(head, tail)
mst_edges.append(cheap_edge[vertex])
mst_edges.append(head_tail_weight)
num_components = num_components - 1
mst = Graph.build(edges=mst_edges)
return mst
2 changes: 1 addition & 1 deletion hashes/md5.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def preprocess(message: bytes) -> bytes:
return bit_string


def get_block_words(bit_string: bytes) -> Generator[list[int], None, None]:
def get_block_words(bit_string: bytes) -> Generator[list[int]]:
"""
Splits bit string into blocks of 512 chars and yields each block as a list
of 32-bit words
Expand Down
4 changes: 2 additions & 2 deletions machine_learning/frequent_pattern_growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def create_tree(data_set: list, min_sup: int = 1) -> tuple[TreeNode, dict]:
if not (freq_item_set := set(header_table)):
return TreeNode("Null Set", 1, None), {}

for k in header_table:
header_table[k] = [header_table[k], None]
for key, value in header_table.items():
header_table[key] = [value, None]

fp_tree = TreeNode("Null Set", 1, None) # Parent is None for the root node
for tran_set in data_set:
Expand Down
2 changes: 1 addition & 1 deletion maths/collatz_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from collections.abc import Generator


def collatz_sequence(n: int) -> Generator[int, None, None]:
def collatz_sequence(n: int) -> Generator[int]:
"""
Generate the Collatz sequence starting at n.
>>> tuple(collatz_sequence(2.1))
Expand Down
6 changes: 3 additions & 3 deletions maths/prime_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections.abc import Generator


def slow_primes(max_n: int) -> Generator[int, None, None]:
def slow_primes(max_n: int) -> Generator[int]:
"""
Return a list of all primes numbers up to max.
>>> list(slow_primes(0))
Expand All @@ -29,7 +29,7 @@ def slow_primes(max_n: int) -> Generator[int, None, None]:
yield i


def primes(max_n: int) -> Generator[int, None, None]:
def primes(max_n: int) -> Generator[int]:
"""
Return a list of all primes numbers up to max.
>>> list(primes(0))
Expand Down Expand Up @@ -58,7 +58,7 @@ def primes(max_n: int) -> Generator[int, None, None]:
yield i


def fast_primes(max_n: int) -> Generator[int, None, None]:
def fast_primes(max_n: int) -> Generator[int]:
"""
Return a list of all primes numbers up to max.
>>> list(fast_primes(0))
Expand Down
2 changes: 1 addition & 1 deletion maths/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations

from math import pi, pow
from math import pi, pow # noqa: A004


def vol_cube(side_length: float) -> float:
Expand Down
10 changes: 4 additions & 6 deletions neural_network/input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ def _extract_images(f):
with gzip.GzipFile(fileobj=f) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
raise ValueError(
"Invalid magic number %d in MNIST image file: %s" % (magic, f.name)
)
msg = f"Invalid magic number {magic} in MNIST image file: {f.name}"
raise ValueError(msg)
num_images = _read32(bytestream)
rows = _read32(bytestream)
cols = _read32(bytestream)
Expand Down Expand Up @@ -102,9 +101,8 @@ def _extract_labels(f, one_hot=False, num_classes=10):
with gzip.GzipFile(fileobj=f) as bytestream:
magic = _read32(bytestream)
if magic != 2049:
raise ValueError(
"Invalid magic number %d in MNIST label file: %s" % (magic, f.name)
)
msg = f"Invalid magic number {magic} in MNIST label file: {f.name}"
raise ValueError(msg)
num_items = _read32(bytestream)
buf = bytestream.read(num_items)
labels = np.frombuffer(buf, dtype=np.uint8)
Expand Down
9 changes: 4 additions & 5 deletions physics/basic_orbital_capture.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from math import pow, sqrt

from scipy.constants import G, c, pi

"""
These two functions will return the radii of impact for a target object
of mass M and radius R as well as it's effective cross sectional area sigma.
Expand All @@ -14,9 +10,12 @@
cross section for capture as sigma=π*R_capture**2.
This algorithm does not account for an N-body problem.
"""

from math import pow, sqrt # noqa: A004

from scipy.constants import G, c, pi


def capture_radii(
target_body_radius: float, target_body_mass: float, projectile_velocity: float
Expand Down
2 changes: 1 addition & 1 deletion physics/grahams_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(Description adapted from https://en.wikipedia.org/wiki/Graham%27s_law)
"""

from math import pow, sqrt
from math import pow, sqrt # noqa: A004


def validate(*values: float) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_025/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from collections.abc import Generator


def fibonacci_generator() -> Generator[int, None, None]:
def fibonacci_generator() -> Generator[int]:
"""
A generator that produces numbers in the Fibonacci sequence
Expand Down
Loading

0 comments on commit 6c61cf0

Please sign in to comment.