Skip to content

Commit 421ace8

Browse files
pre-commit-ci[bot]github-actionscclauss
authored
[pre-commit.ci] pre-commit autoupdate (TheAlgorithms#9013)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.285 → v0.0.286](astral-sh/ruff-pre-commit@v0.0.285...v0.0.286) - [github.com/tox-dev/pyproject-fmt: 0.13.1 → 1.1.0](tox-dev/pyproject-fmt@0.13.1...1.1.0) * updating DIRECTORY.md * Fis ruff rules PIE808,PLR1714 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 0a94380 commit 421ace8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+70
-71
lines changed

.pre-commit-config.yaml

+2-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.0.285
19+
rev: v0.0.286
2020
hooks:
2121
- id: ruff
2222

@@ -33,7 +33,7 @@ repos:
3333
- tomli
3434

3535
- repo: https://github.com/tox-dev/pyproject-fmt
36-
rev: "0.13.1"
36+
rev: "1.1.0"
3737
hooks:
3838
- id: pyproject-fmt
3939

DIRECTORY.md

-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@
245245
* Stacks
246246
* [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py)
247247
* [Dijkstras Two Stack Algorithm](data_structures/stacks/dijkstras_two_stack_algorithm.py)
248-
* [Evaluate Postfix Notations](data_structures/stacks/evaluate_postfix_notations.py)
249248
* [Infix To Postfix Conversion](data_structures/stacks/infix_to_postfix_conversion.py)
250249
* [Infix To Prefix Conversion](data_structures/stacks/infix_to_prefix_conversion.py)
251250
* [Next Greater Element](data_structures/stacks/next_greater_element.py)

arithmetic_analysis/jacobi_iteration_method.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ def strictly_diagonally_dominant(table: NDArray[float64]) -> bool:
152152

153153
is_diagonally_dominant = True
154154

155-
for i in range(0, rows):
155+
for i in range(rows):
156156
total = 0
157-
for j in range(0, cols - 1):
157+
for j in range(cols - 1):
158158
if i == j:
159159
continue
160160
else:

arithmetic_analysis/secant_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float
2020
"""
2121
x0 = lower_bound
2222
x1 = upper_bound
23-
for _ in range(0, repeats):
23+
for _ in range(repeats):
2424
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
2525
return x1
2626

backtracking/hamiltonian_cycle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int)
9595
return graph[path[curr_ind - 1]][path[0]] == 1
9696

9797
# Recursive Step
98-
for next_ver in range(0, len(graph)):
98+
for next_ver in range(len(graph)):
9999
if valid_connection(graph, next_ver, curr_ind, path):
100100
# Insert current vertex into path as next transition
101101
path[curr_ind] = next_ver

backtracking/sudoku.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
4848
is found) else returns True if it is 'safe'
4949
"""
5050
for i in range(9):
51-
if grid[row][i] == n or grid[i][column] == n:
51+
if n in {grid[row][i], grid[i][column]}:
5252
return False
5353

5454
for i in range(3):

bit_manipulation/reverse_bits.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def get_reverse_bit_string(number: int) -> str:
2020
)
2121
raise TypeError(msg)
2222
bit_string = ""
23-
for _ in range(0, 32):
23+
for _ in range(32):
2424
bit_string += str(number % 2)
2525
number = number >> 1
2626
return bit_string

ciphers/trafid_cipher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def decrypt_message(
119119
for i in range(0, len(message) + 1, period):
120120
a, b, c = __decrypt_part(message[i : i + period], character_to_number)
121121

122-
for j in range(0, len(a)):
122+
for j in range(len(a)):
123123
decrypted_numeric.append(a[j] + b[j] + c[j])
124124

125125
for each in decrypted_numeric:

data_structures/binary_tree/lazy_segment_tree.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class SegmentTree:
77
def __init__(self, size: int) -> None:
88
self.size = size
99
# approximate the overall size of segment tree with given value
10-
self.segment_tree = [0 for i in range(0, 4 * size)]
10+
self.segment_tree = [0 for i in range(4 * size)]
1111
# create array to store lazy update
12-
self.lazy = [0 for i in range(0, 4 * size)]
13-
self.flag = [0 for i in range(0, 4 * size)] # flag for lazy update
12+
self.lazy = [0 for i in range(4 * size)]
13+
self.flag = [0 for i in range(4 * size)] # flag for lazy update
1414

1515
def left(self, idx: int) -> int:
1616
"""

data_structures/linked_list/circular_linked_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_circular_linked_list() -> None:
125125
circular_linked_list.insert_tail(6)
126126
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7))
127127
circular_linked_list.insert_head(0)
128-
assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7))
128+
assert str(circular_linked_list) == "->".join(str(i) for i in range(7))
129129

130130
assert circular_linked_list.delete_front() == 0
131131
assert circular_linked_list.delete_tail() == 6

data_structures/linked_list/doubly_linked_list.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def insert_at_nth(self, index: int, data):
9898
self.tail = new_node
9999
else:
100100
temp = self.head
101-
for _ in range(0, index):
101+
for _ in range(index):
102102
temp = temp.next
103103
temp.previous.next = new_node
104104
new_node.previous = temp.previous
@@ -149,7 +149,7 @@ def delete_at_nth(self, index: int):
149149
self.tail.next = None
150150
else:
151151
temp = self.head
152-
for _ in range(0, index):
152+
for _ in range(index):
153153
temp = temp.next
154154
delete_node = temp
155155
temp.next.previous = temp.previous
@@ -215,7 +215,7 @@ def test_doubly_linked_list() -> None:
215215

216216
linked_list.insert_at_head(0)
217217
linked_list.insert_at_tail(11)
218-
assert str(linked_list) == "->".join(str(i) for i in range(0, 12))
218+
assert str(linked_list) == "->".join(str(i) for i in range(12))
219219

220220
assert linked_list.delete_head() == 0
221221
assert linked_list.delete_at_nth(9) == 10

data_structures/linked_list/is_palindrome.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def is_palindrome_dict(head):
6868
middle += 1
6969
else:
7070
step = 0
71-
for i in range(0, len(v)):
71+
for i in range(len(v)):
7272
if v[i] + v[len(v) - 1 - step] != checksum:
7373
return False
7474
step += 1

data_structures/linked_list/singly_linked_list.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,19 @@ def test_singly_linked_list() -> None:
370370

371371
linked_list.insert_head(0)
372372
linked_list.insert_tail(11)
373-
assert str(linked_list) == "->".join(str(i) for i in range(0, 12))
373+
assert str(linked_list) == "->".join(str(i) for i in range(12))
374374

375375
assert linked_list.delete_head() == 0
376376
assert linked_list.delete_nth(9) == 10
377377
assert linked_list.delete_tail() == 11
378378
assert len(linked_list) == 9
379379
assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
380380

381-
assert all(linked_list[i] == i + 1 for i in range(0, 9)) is True
381+
assert all(linked_list[i] == i + 1 for i in range(9)) is True
382382

383-
for i in range(0, 9):
383+
for i in range(9):
384384
linked_list[i] = -i
385-
assert all(linked_list[i] == -i for i in range(0, 9)) is True
385+
assert all(linked_list[i] == -i for i in range(9)) is True
386386

387387
linked_list.reverse()
388388
assert str(linked_list) == "->".join(str(i) for i in range(-8, 1))

data_structures/stacks/stock_span_problem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def calculation_span(price, s):
3636

3737
# A utility function to print elements of array
3838
def print_array(arr, n):
39-
for i in range(0, n):
39+
for i in range(n):
4040
print(arr[i], end=" ")
4141

4242

digital_image_processing/filters/bilateral_filter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def get_slice(img: np.ndarray, x: int, y: int, kernel_size: int) -> np.ndarray:
3131
def get_gauss_kernel(kernel_size: int, spatial_variance: float) -> np.ndarray:
3232
# Creates a gaussian kernel of given dimension.
3333
arr = np.zeros((kernel_size, kernel_size))
34-
for i in range(0, kernel_size):
35-
for j in range(0, kernel_size):
34+
for i in range(kernel_size):
35+
for j in range(kernel_size):
3636
arr[i, j] = math.sqrt(
3737
abs(i - kernel_size // 2) ** 2 + abs(j - kernel_size // 2) ** 2
3838
)

digital_image_processing/filters/convolve.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def im2col(image, block_size):
1111
dst_width = rows - block_size[0] + 1
1212
image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0]))
1313
row = 0
14-
for i in range(0, dst_height):
15-
for j in range(0, dst_width):
14+
for i in range(dst_height):
15+
for j in range(dst_width):
1616
window = ravel(image[i : i + block_size[0], j : j + block_size[1]])
1717
image_array[row, :] = window
1818
row += 1

digital_image_processing/filters/local_binary_pattern.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int)
7171

7272
# Iterating through the image and calculating the
7373
# local binary pattern value for each pixel.
74-
for i in range(0, image.shape[0]):
75-
for j in range(0, image.shape[1]):
74+
for i in range(image.shape[0]):
75+
for j in range(image.shape[1]):
7676
lbp_image[i][j] = local_binary_value(image, i, j)
7777

7878
cv2.imshow("local binary pattern", lbp_image)

digital_image_processing/test_digital_image_processing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def test_local_binary_pattern():
118118

119119
# Iterating through the image and calculating the local binary pattern value
120120
# for each pixel.
121-
for i in range(0, image.shape[0]):
122-
for j in range(0, image.shape[1]):
121+
for i in range(image.shape[0]):
122+
for j in range(image.shape[1]):
123123
lbp_image[i][j] = lbp.local_binary_value(image, i, j)
124124

125125
assert lbp_image.any()

divide_and_conquer/strassen_matrix_multiplication.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
131131

132132
# Adding zeros to the matrices so that the arrays dimensions are the same and also
133133
# power of 2
134-
for i in range(0, maxim):
134+
for i in range(maxim):
135135
if i < dimension1[0]:
136136
for _ in range(dimension1[1], maxim):
137137
new_matrix1[i].append(0)
@@ -146,7 +146,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
146146
final_matrix = actual_strassen(new_matrix1, new_matrix2)
147147

148148
# Removing the additional zeros
149-
for i in range(0, maxim):
149+
for i in range(maxim):
150150
if i < dimension1[0]:
151151
for _ in range(dimension2[1], maxim):
152152
final_matrix[i].pop()

dynamic_programming/floyd_warshall.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ class Graph:
55
def __init__(self, n=0): # a graph with Node 0,1,...,N-1
66
self.n = n
77
self.w = [
8-
[math.inf for j in range(0, n)] for i in range(0, n)
8+
[math.inf for j in range(n)] for i in range(n)
99
] # adjacency matrix for weight
1010
self.dp = [
11-
[math.inf for j in range(0, n)] for i in range(0, n)
11+
[math.inf for j in range(n)] for i in range(n)
1212
] # dp[i][j] stores minimum distance from i to j
1313

1414
def add_edge(self, u, v, w):
1515
self.dp[u][v] = w
1616

1717
def floyd_warshall(self):
18-
for k in range(0, self.n):
19-
for i in range(0, self.n):
20-
for j in range(0, self.n):
18+
for k in range(self.n):
19+
for i in range(self.n):
20+
for j in range(self.n):
2121
self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j])
2222

2323
def show_min(self, u, v):

hashes/chaos_machine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def xorshift(x, y):
5353
key = machine_time % m
5454

5555
# Evolution (Time Length)
56-
for _ in range(0, t):
56+
for _ in range(t):
5757
# Variables (Position + Parameters)
5858
r = params_space[key]
5959
value = buffer_space[key]

hashes/hamming_code.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def emitter_converter(size_par, data):
135135

136136
# Mount the message
137137
cont_bp = 0 # parity bit counter
138-
for x in range(0, size_par + len(data)):
138+
for x in range(size_par + len(data)):
139139
if data_ord[x] is None:
140140
data_out.append(str(parity[cont_bp]))
141141
cont_bp += 1
@@ -228,7 +228,7 @@ def receptor_converter(size_par, data):
228228

229229
# Mount the message
230230
cont_bp = 0 # Parity bit counter
231-
for x in range(0, size_par + len(data_output)):
231+
for x in range(size_par + len(data_output)):
232232
if data_ord[x] is None:
233233
data_out.append(str(parity[cont_bp]))
234234
cont_bp += 1

hashes/sha1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def final_hash(self):
9797
for block in self.blocks:
9898
expanded_block = self.expand_block(block)
9999
a, b, c, d, e = self.h
100-
for i in range(0, 80):
100+
for i in range(80):
101101
if 0 <= i < 20:
102102
f = (b & c) | ((~b) & d)
103103
k = 0x5A827999

hashes/sha256.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def final_hash(self) -> None:
138138

139139
a, b, c, d, e, f, g, h = self.hashes
140140

141-
for index in range(0, 64):
141+
for index in range(64):
142142
if index > 15:
143143
# modify the zero-ed indexes at the end of the array
144144
s0 = (

machine_learning/gradient_descent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def run_gradient_descent():
110110
while True:
111111
j += 1
112112
temp_parameter_vector = [0, 0, 0, 0]
113-
for i in range(0, len(parameter_vector)):
113+
for i in range(len(parameter_vector)):
114114
cost_derivative = get_cost_derivative(i - 1)
115115
temp_parameter_vector[i] = (
116116
parameter_vector[i] - LEARNING_RATE * cost_derivative

machine_learning/linear_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def run_linear_regression(data_x, data_y):
7878

7979
theta = np.zeros((1, no_features))
8080

81-
for i in range(0, iterations):
81+
for i in range(iterations):
8282
theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
8383
error = sum_of_square_error(data_x, data_y, len_data, theta)
8484
print(f"At Iteration {i + 1} - Error is {error:.5f}")
@@ -107,7 +107,7 @@ def main():
107107
theta = run_linear_regression(data_x, data_y)
108108
len_result = theta.shape[1]
109109
print("Resultant Feature vector : ")
110-
for i in range(0, len_result):
110+
for i in range(len_result):
111111
print(f"{theta[0, i]:.5f}")
112112

113113

machine_learning/lstm/lstm_prediction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
train_x, train_y = [], []
3333
test_x, test_y = [], []
3434

35-
for i in range(0, len(train_data) - forward_days - look_back + 1):
35+
for i in range(len(train_data) - forward_days - look_back + 1):
3636
train_x.append(train_data[i : i + look_back])
3737
train_y.append(train_data[i + look_back : i + look_back + forward_days])
38-
for i in range(0, len(test_data) - forward_days - look_back + 1):
38+
for i in range(len(test_data) - forward_days - look_back + 1):
3939
test_x.append(test_data[i : i + look_back])
4040
test_y.append(test_data[i + look_back : i + look_back + forward_days])
4141
x_train = np.array(train_x)

maths/entropy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def analyze_text(text: str) -> tuple[dict, dict]:
101101

102102
# first case when we have space at start.
103103
two_char_strings[" " + text[0]] += 1
104-
for i in range(0, len(text) - 1):
104+
for i in range(len(text) - 1):
105105
single_char_strings[text[i]] += 1
106106
two_char_strings[text[i : i + 2]] += 1
107107
return single_char_strings, two_char_strings

maths/eulers_totient.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def totient(n: int) -> list:
2121
for i in range(2, n + 1):
2222
if is_prime[i]:
2323
primes.append(i)
24-
for j in range(0, len(primes)):
24+
for j in range(len(primes)):
2525
if i * primes[j] >= n:
2626
break
2727
is_prime[i * primes[j]] = False

maths/greedy_coin_change.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def find_minimum_change(denominations: list[int], value: str) -> list[int]:
8181
):
8282
n = int(input("Enter the number of denominations you want to add: ").strip())
8383

84-
for i in range(0, n):
84+
for i in range(n):
8585
denominations.append(int(input(f"Denomination {i}: ").strip()))
8686
value = input("Enter the change you want to make in Indian Currency: ").strip()
8787
else:

maths/persistence.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def multiplicative_persistence(num: int) -> int:
2828
numbers = [int(i) for i in num_string]
2929

3030
total = 1
31-
for i in range(0, len(numbers)):
31+
for i in range(len(numbers)):
3232
total *= numbers[i]
3333

3434
num_string = str(total)
@@ -67,7 +67,7 @@ def additive_persistence(num: int) -> int:
6767
numbers = [int(i) for i in num_string]
6868

6969
total = 0
70-
for i in range(0, len(numbers)):
70+
for i in range(len(numbers)):
7171
total += numbers[i]
7272

7373
num_string = str(total)

0 commit comments

Comments
 (0)