Skip to content

Commit 4fe50bc

Browse files
[pre-commit.ci] pre-commit autoupdate -- ruff 2025 stable format (TheAlgorithms#12521)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.8.6 → v0.9.1](astral-sh/ruff-pre-commit@v0.8.6...v0.9.1) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/dual_number_automatic_differentiation.py * Update maths/dual_number_automatic_differentiation.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dual_number_automatic_differentiation.py * Update dual_number_automatic_differentiation.py * No <fin-streamer> tag with the specified data-test attribute found. --------- 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 cfcc84e commit 4fe50bc

23 files changed

+93
-91
lines changed

.pre-commit-config.yaml

+1-1
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.8.6
19+
rev: v0.9.1
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format

ciphers/base64_cipher.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ def base64_decode(encoded_data: str) -> bytes:
105105

106106
# Check if the encoded string contains non base64 characters
107107
if padding:
108-
assert all(
109-
char in B64_CHARSET for char in encoded_data[:-padding]
110-
), "Invalid base64 character(s) found."
108+
assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (
109+
"Invalid base64 character(s) found."
110+
)
111111
else:
112-
assert all(
113-
char in B64_CHARSET for char in encoded_data
114-
), "Invalid base64 character(s) found."
112+
assert all(char in B64_CHARSET for char in encoded_data), (
113+
"Invalid base64 character(s) found."
114+
)
115115

116116
# Check the padding
117117
assert len(encoded_data) % 4 == 0 and padding < 3, "Incorrect padding"

ciphers/caesar_cipher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
225225

226226
if __name__ == "__main__":
227227
while True:
228-
print(f'\n{"-" * 10}\n Menu\n{"-" * 10}')
228+
print(f"\n{'-' * 10}\n Menu\n{'-' * 10}")
229229
print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
230230

231231
# get user input

computer_vision/flip_augmentation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main() -> None:
3333
file_name = paths[index].split(os.sep)[-1].rsplit(".", 1)[0]
3434
file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}"
3535
cv2.imwrite(f"{file_root}.jpg", image, [cv2.IMWRITE_JPEG_QUALITY, 85])
36-
print(f"Success {index+1}/{len(new_images)} with {file_name}")
36+
print(f"Success {index + 1}/{len(new_images)} with {file_name}")
3737
annos_list = []
3838
for anno in new_annos[index]:
3939
obj = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}"

computer_vision/mosaic_augmentation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def main() -> None:
4141
file_name = path.split(os.sep)[-1].rsplit(".", 1)[0]
4242
file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}"
4343
cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85])
44-
print(f"Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}")
44+
print(f"Succeeded {index + 1}/{NUMBER_IMAGES} with {file_name}")
4545
annos_list = []
4646
for anno in new_annos:
4747
width = anno[3] - anno[1]

data_structures/hashing/number_theory/prime_numbers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def is_prime(number: int) -> bool:
3232
"""
3333

3434
# precondition
35-
assert isinstance(number, int) and (
36-
number >= 0
37-
), "'number' must been an int and positive"
35+
assert isinstance(number, int) and (number >= 0), (
36+
"'number' must been an int and positive"
37+
)
3838

3939
if 1 < number < 4:
4040
# 2 and 3 are primes

data_structures/heap/min_heap.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def is_empty(self):
124124
return len(self.heap) == 0
125125

126126
def decrease_key(self, node, new_value):
127-
assert (
128-
self.heap[self.idx_of_element[node]].val > new_value
129-
), "newValue must be less that current value"
127+
assert self.heap[self.idx_of_element[node]].val > new_value, (
128+
"newValue must be less that current value"
129+
)
130130
node.val = new_value
131131
self.heap_dict[node.name] = new_value
132132
self.sift_up(self.idx_of_element[node])

data_structures/kd_tree/tests/test_kdtree.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
4848
assert kdtree is not None, "Expected a KDNode, got None"
4949

5050
# Check if root has correct dimensions
51-
assert (
52-
len(kdtree.point) == num_dimensions
53-
), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
51+
assert len(kdtree.point) == num_dimensions, (
52+
f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
53+
)
5454

5555
# Check that the tree is balanced to some extent (simplistic check)
56-
assert isinstance(
57-
kdtree, KDNode
58-
), f"Expected KDNode instance, got {type(kdtree)}"
56+
assert isinstance(kdtree, KDNode), (
57+
f"Expected KDNode instance, got {type(kdtree)}"
58+
)
5959

6060

6161
def test_nearest_neighbour_search():

data_structures/suffix_tree/tests/test_suffix_tree.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,37 @@ def test_search_existing_patterns(self) -> None:
2222
patterns = ["ana", "ban", "na"]
2323
for pattern in patterns:
2424
with self.subTest(pattern=pattern):
25-
assert self.suffix_tree.search(
26-
pattern
27-
), f"Pattern '{pattern}' should be found."
25+
assert self.suffix_tree.search(pattern), (
26+
f"Pattern '{pattern}' should be found."
27+
)
2828

2929
def test_search_non_existing_patterns(self) -> None:
3030
"""Test searching for patterns that do not exist in the suffix tree."""
3131
patterns = ["xyz", "apple", "cat"]
3232
for pattern in patterns:
3333
with self.subTest(pattern=pattern):
34-
assert not self.suffix_tree.search(
35-
pattern
36-
), f"Pattern '{pattern}' should not be found."
34+
assert not self.suffix_tree.search(pattern), (
35+
f"Pattern '{pattern}' should not be found."
36+
)
3737

3838
def test_search_empty_pattern(self) -> None:
3939
"""Test searching for an empty pattern."""
4040
assert self.suffix_tree.search(""), "An empty pattern should be found."
4141

4242
def test_search_full_text(self) -> None:
4343
"""Test searching for the full text."""
44-
assert self.suffix_tree.search(
45-
self.text
46-
), "The full text should be found in the suffix tree."
44+
assert self.suffix_tree.search(self.text), (
45+
"The full text should be found in the suffix tree."
46+
)
4747

4848
def test_search_substrings(self) -> None:
4949
"""Test searching for substrings of the full text."""
5050
substrings = ["ban", "ana", "a", "na"]
5151
for substring in substrings:
5252
with self.subTest(substring=substring):
53-
assert self.suffix_tree.search(
54-
substring
55-
), f"Substring '{substring}' should be found."
53+
assert self.suffix_tree.search(substring), (
54+
f"Substring '{substring}' should be found."
55+
)
5656

5757

5858
if __name__ == "__main__":

dynamic_programming/climbing_stairs.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def climb_stairs(number_of_steps: int) -> int:
2525
...
2626
AssertionError: number_of_steps needs to be positive integer, your input -7
2727
"""
28-
assert (
29-
isinstance(number_of_steps, int) and number_of_steps > 0
30-
), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
28+
assert isinstance(number_of_steps, int) and number_of_steps > 0, (
29+
f"number_of_steps needs to be positive integer, your input {number_of_steps}"
30+
)
3131
if number_of_steps == 1:
3232
return 1
3333
previous, current = 1, 1

dynamic_programming/iterating_through_submasks.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def list_of_submasks(mask: int) -> list[int]:
3737
3838
"""
3939

40-
assert (
41-
isinstance(mask, int) and mask > 0
42-
), f"mask needs to be positive integer, your input {mask}"
40+
assert isinstance(mask, int) and mask > 0, (
41+
f"mask needs to be positive integer, your input {mask}"
42+
)
4343

4444
"""
4545
first submask iterated will be mask itself then operation will be performed

dynamic_programming/matrix_chain_multiplication.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def elapsed_time(msg: str) -> Iterator:
134134

135135
start = perf_counter_ns()
136136
yield
137-
print(f"Finished: {msg} in {(perf_counter_ns() - start) / 10 ** 9} seconds.")
137+
print(f"Finished: {msg} in {(perf_counter_ns() - start) / 10**9} seconds.")
138138

139139

140140
if __name__ == "__main__":

machine_learning/linear_discriminant_analysis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def main():
322322
user_count = valid_input(
323323
input_type=int,
324324
condition=lambda x: x > 0,
325-
input_msg=(f"Enter The number of instances for class_{i+1}: "),
325+
input_msg=(f"Enter The number of instances for class_{i + 1}: "),
326326
err_msg="Number of instances should be positive!",
327327
)
328328
counts.append(user_count)
@@ -333,7 +333,7 @@ def main():
333333
for a in range(n_classes):
334334
user_mean = valid_input(
335335
input_type=float,
336-
input_msg=(f"Enter the value of mean for class_{a+1}: "),
336+
input_msg=(f"Enter the value of mean for class_{a + 1}: "),
337337
err_msg="This is an invalid value.",
338338
)
339339
user_means.append(user_mean)

maths/dual_number_automatic_differentiation.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def __init__(self, real, rank):
1717
self.duals = rank
1818

1919
def __repr__(self):
20-
return (
21-
f"{self.real}+"
22-
f"{'+'.join(str(dual)+'E'+str(n+1)for n,dual in enumerate(self.duals))}"
23-
)
20+
s = "+".join(f"{dual}E{n}" for n, dual in enumerate(self.duals, 1))
21+
return f"{self.real}+{s}"
2422

2523
def reduce(self):
2624
cur = self.duals.copy()

maths/max_sum_sliding_window.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ def max_sum_in_array(array: list[int], k: int) -> int:
4343
testmod()
4444
array = [randint(-1000, 1000) for i in range(100)]
4545
k = randint(0, 110)
46-
print(f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}")
46+
print(
47+
f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array, k)}"
48+
)

maths/numerical_analysis/integration_by_simpson_approx.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ def simpson_integration(function, a: float, b: float, precision: int = 4) -> flo
8888
AssertionError: precision should be positive integer your input : -1
8989
9090
"""
91-
assert callable(
92-
function
93-
), f"the function(object) passed should be callable your input : {function}"
91+
assert callable(function), (
92+
f"the function(object) passed should be callable your input : {function}"
93+
)
9494
assert isinstance(a, (float, int)), f"a should be float or integer your input : {a}"
9595
assert isinstance(function(a), (float, int)), (
9696
"the function should return integer or float return type of your function, "
9797
f"{type(a)}"
9898
)
9999
assert isinstance(b, (float, int)), f"b should be float or integer your input : {b}"
100-
assert (
101-
isinstance(precision, int) and precision > 0
102-
), f"precision should be positive integer your input : {precision}"
100+
assert isinstance(precision, int) and precision > 0, (
101+
f"precision should be positive integer your input : {precision}"
102+
)
103103

104104
# just applying the formula of simpson for approximate integration written in
105105
# mentioned article in first comment of this file and above this function

maths/prime_check.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ def test_primes(self):
7373
def test_not_primes(self):
7474
with pytest.raises(ValueError):
7575
is_prime(-19)
76-
assert not is_prime(
77-
0
78-
), "Zero doesn't have any positive factors, primes must have exactly two."
79-
assert not is_prime(
80-
1
81-
), "One only has 1 positive factor, primes must have exactly two."
76+
assert not is_prime(0), (
77+
"Zero doesn't have any positive factors, primes must have exactly two."
78+
)
79+
assert not is_prime(1), (
80+
"One only has 1 positive factor, primes must have exactly two."
81+
)
8282
assert not is_prime(2 * 2)
8383
assert not is_prime(2 * 3)
8484
assert not is_prime(3 * 3)

maths/primelib.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def is_prime(number: int) -> bool:
6666
"""
6767

6868
# precondition
69-
assert isinstance(number, int) and (
70-
number >= 0
71-
), "'number' must been an int and positive"
69+
assert isinstance(number, int) and (number >= 0), (
70+
"'number' must been an int and positive"
71+
)
7272

7373
status = True
7474

@@ -254,9 +254,9 @@ def greatest_prime_factor(number):
254254
"""
255255

256256
# precondition
257-
assert isinstance(number, int) and (
258-
number >= 0
259-
), "'number' must been an int and >= 0"
257+
assert isinstance(number, int) and (number >= 0), (
258+
"'number' must been an int and >= 0"
259+
)
260260

261261
ans = 0
262262

@@ -296,9 +296,9 @@ def smallest_prime_factor(number):
296296
"""
297297

298298
# precondition
299-
assert isinstance(number, int) and (
300-
number >= 0
301-
), "'number' must been an int and >= 0"
299+
assert isinstance(number, int) and (number >= 0), (
300+
"'number' must been an int and >= 0"
301+
)
302302

303303
ans = 0
304304

@@ -399,9 +399,9 @@ def goldbach(number):
399399
"""
400400

401401
# precondition
402-
assert (
403-
isinstance(number, int) and (number > 2) and is_even(number)
404-
), "'number' must been an int, even and > 2"
402+
assert isinstance(number, int) and (number > 2) and is_even(number), (
403+
"'number' must been an int, even and > 2"
404+
)
405405

406406
ans = [] # this list will returned
407407

@@ -525,9 +525,9 @@ def kg_v(number1, number2):
525525
done.append(n)
526526

527527
# precondition
528-
assert isinstance(ans, int) and (
529-
ans >= 0
530-
), "'ans' must been from type int and positive"
528+
assert isinstance(ans, int) and (ans >= 0), (
529+
"'ans' must been from type int and positive"
530+
)
531531

532532
return ans
533533

@@ -574,9 +574,9 @@ def get_prime(n):
574574
ans += 1
575575

576576
# precondition
577-
assert isinstance(ans, int) and is_prime(
578-
ans
579-
), "'ans' must been a prime number and from type int"
577+
assert isinstance(ans, int) and is_prime(ans), (
578+
"'ans' must been a prime number and from type int"
579+
)
580580

581581
return ans
582582

@@ -705,9 +705,9 @@ def is_perfect_number(number):
705705
"""
706706

707707
# precondition
708-
assert isinstance(number, int) and (
709-
number > 1
710-
), "'number' must been an int and >= 1"
708+
assert isinstance(number, int) and (number > 1), (
709+
"'number' must been an int and >= 1"
710+
)
711711

712712
divisors = get_divisors(number)
713713

matrix/matrix_based_game.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def process_game(size: int, matrix: list[str], moves: list[tuple[int, int]]) ->
273273
size = int(input("Enter the size of the matrix: "))
274274
validate_matrix_size(size)
275275
print(f"Enter the {size} rows of the matrix:")
276-
matrix = [input(f"Row {i+1}: ") for i in range(size)]
276+
matrix = [input(f"Row {i + 1}: ") for i in range(size)]
277277
validate_matrix_content(matrix, size)
278278
moves_input = input("Enter the moves (e.g., '0 0, 1 1'): ")
279279
moves = parse_moves(moves_input)

neural_network/input_data.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def __init__(
160160
self._num_examples = 10000
161161
self.one_hot = one_hot
162162
else:
163-
assert (
164-
images.shape[0] == labels.shape[0]
165-
), f"images.shape: {images.shape} labels.shape: {labels.shape}"
163+
assert images.shape[0] == labels.shape[0], (
164+
f"images.shape: {images.shape} labels.shape: {labels.shape}"
165+
)
166166
self._num_examples = images.shape[0]
167167

168168
# Convert shape from [num examples, rows, columns, depth]

scripts/validate_solutions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ def test_project_euler(solution_path: pathlib.Path) -> None:
9494
solution_module = convert_path_to_module(solution_path)
9595
answer = str(solution_module.solution())
9696
answer = hashlib.sha256(answer.encode()).hexdigest()
97-
assert (
98-
answer == expected
99-
), f"Expected solution to {problem_number} to have hash {expected}, got {answer}"
97+
assert answer == expected, (
98+
f"Expected solution to {problem_number} to have hash {expected}, got {answer}"
99+
)

0 commit comments

Comments
 (0)