Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a42eb35

Browse files
MaximSmolskiypre-commit-ci[bot]
andauthoredApr 19, 2024
Enable ruff E741 rule (TheAlgorithms#11370)
* Enable ruff E741 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0a9a860 commit a42eb35

File tree

14 files changed

+102
-92
lines changed

14 files changed

+102
-92
lines changed
 

‎data_structures/binary_tree/non_recursive_segment_tree.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ def update(self, p: int, v: T) -> None:
8787
p = p // 2
8888
self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1])
8989

90-
def query(self, l: int, r: int) -> T | None:
90+
def query(self, left: int, right: int) -> T | None:
9191
"""
9292
Get range query value in log(N) time
93-
:param l: left element index
94-
:param r: right element index
95-
:return: element combined in the range [l, r]
93+
:param left: left element index
94+
:param right: right element index
95+
:return: element combined in the range [left, right]
9696
9797
>>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b)
9898
>>> st.query(0, 2)
@@ -104,15 +104,15 @@ def query(self, l: int, r: int) -> T | None:
104104
>>> st.query(2, 3)
105105
7
106106
"""
107-
l, r = l + self.N, r + self.N
107+
left, right = left + self.N, right + self.N
108108

109109
res: T | None = None
110-
while l <= r:
111-
if l % 2 == 1:
112-
res = self.st[l] if res is None else self.fn(res, self.st[l])
113-
if r % 2 == 0:
114-
res = self.st[r] if res is None else self.fn(res, self.st[r])
115-
l, r = (l + 1) // 2, (r - 1) // 2
110+
while left <= right:
111+
if left % 2 == 1:
112+
res = self.st[left] if res is None else self.fn(res, self.st[left])
113+
if right % 2 == 0:
114+
res = self.st[right] if res is None else self.fn(res, self.st[right])
115+
left, right = (left + 1) // 2, (right - 1) // 2
116116
return res
117117

118118

‎data_structures/binary_tree/segment_tree.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def right(self, idx):
3535
"""
3636
return idx * 2 + 1
3737

38-
def build(self, idx, l, r):
39-
if l == r:
40-
self.st[idx] = self.A[l]
38+
def build(self, idx, left, right):
39+
if left == right:
40+
self.st[idx] = self.A[left]
4141
else:
42-
mid = (l + r) // 2
43-
self.build(self.left(idx), l, mid)
44-
self.build(self.right(idx), mid + 1, r)
42+
mid = (left + right) // 2
43+
self.build(self.left(idx), left, mid)
44+
self.build(self.right(idx), mid + 1, right)
4545
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
4646

4747
def update(self, a, b, val):
@@ -56,18 +56,18 @@ def update(self, a, b, val):
5656
"""
5757
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
5858

59-
def update_recursive(self, idx, l, r, a, b, val):
59+
def update_recursive(self, idx, left, right, a, b, val):
6060
"""
6161
update(1, 1, N, a, b, v) for update val v to [a,b]
6262
"""
63-
if r < a or l > b:
63+
if right < a or left > b:
6464
return True
65-
if l == r:
65+
if left == right:
6666
self.st[idx] = val
6767
return True
68-
mid = (l + r) // 2
69-
self.update_recursive(self.left(idx), l, mid, a, b, val)
70-
self.update_recursive(self.right(idx), mid + 1, r, a, b, val)
68+
mid = (left + right) // 2
69+
self.update_recursive(self.left(idx), left, mid, a, b, val)
70+
self.update_recursive(self.right(idx), mid + 1, right, a, b, val)
7171
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
7272
return True
7373

@@ -83,17 +83,17 @@ def query(self, a, b):
8383
"""
8484
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
8585

86-
def query_recursive(self, idx, l, r, a, b):
86+
def query_recursive(self, idx, left, right, a, b):
8787
"""
8888
query(1, 1, N, a, b) for query max of [a,b]
8989
"""
90-
if r < a or l > b:
90+
if right < a or left > b:
9191
return -math.inf
92-
if l >= a and r <= b:
92+
if left >= a and right <= b:
9393
return self.st[idx]
94-
mid = (l + r) // 2
95-
q1 = self.query_recursive(self.left(idx), l, mid, a, b)
96-
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
94+
mid = (left + right) // 2
95+
q1 = self.query_recursive(self.left(idx), left, mid, a, b)
96+
q2 = self.query_recursive(self.right(idx), mid + 1, right, a, b)
9797
return max(q1, q2)
9898

9999
def show_data(self):

‎data_structures/heap/min_heap.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ def build_heap(self, array):
6666
# this is min-heapify method
6767
def sift_down(self, idx, array):
6868
while True:
69-
l = self.get_left_child_idx(idx)
70-
r = self.get_right_child_idx(idx)
69+
left = self.get_left_child_idx(idx)
70+
right = self.get_right_child_idx(idx)
7171

7272
smallest = idx
73-
if l < len(array) and array[l] < array[idx]:
74-
smallest = l
75-
if r < len(array) and array[r] < array[smallest]:
76-
smallest = r
73+
if left < len(array) and array[left] < array[idx]:
74+
smallest = left
75+
if right < len(array) and array[right] < array[smallest]:
76+
smallest = right
7777

7878
if smallest != idx:
7979
array[idx], array[smallest] = array[smallest], array[idx]

‎dynamic_programming/longest_common_subsequence.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,30 @@ def longest_common_subsequence(x: str, y: str):
3838
n = len(y)
3939

4040
# declaring the array for storing the dp values
41-
l = [[0] * (n + 1) for _ in range(m + 1)]
41+
dp = [[0] * (n + 1) for _ in range(m + 1)]
4242

4343
for i in range(1, m + 1):
4444
for j in range(1, n + 1):
4545
match = 1 if x[i - 1] == y[j - 1] else 0
4646

47-
l[i][j] = max(l[i - 1][j], l[i][j - 1], l[i - 1][j - 1] + match)
47+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + match)
4848

4949
seq = ""
5050
i, j = m, n
5151
while i > 0 and j > 0:
5252
match = 1 if x[i - 1] == y[j - 1] else 0
5353

54-
if l[i][j] == l[i - 1][j - 1] + match:
54+
if dp[i][j] == dp[i - 1][j - 1] + match:
5555
if match == 1:
5656
seq = x[i - 1] + seq
5757
i -= 1
5858
j -= 1
59-
elif l[i][j] == l[i - 1][j]:
59+
elif dp[i][j] == dp[i - 1][j]:
6060
i -= 1
6161
else:
6262
j -= 1
6363

64-
return l[m][n], seq
64+
return dp[m][n], seq
6565

6666

6767
if __name__ == "__main__":

‎dynamic_programming/longest_increasing_subsequence_o_nlogn.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
from __future__ import annotations
88

99

10-
def ceil_index(v, l, r, key):
11-
while r - l > 1:
12-
m = (l + r) // 2
13-
if v[m] >= key:
14-
r = m
10+
def ceil_index(v, left, right, key):
11+
while right - left > 1:
12+
middle = (left + right) // 2
13+
if v[middle] >= key:
14+
right = middle
1515
else:
16-
l = m
17-
return r
16+
left = middle
17+
return right
1818

1919

2020
def longest_increasing_subsequence_length(v: list[int]) -> int:

‎graphs/articulation_points.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Finding Articulation Points in Undirected Graph
2-
def compute_ap(l):
3-
n = len(l)
2+
def compute_ap(graph):
3+
n = len(graph)
44
out_edge_count = 0
55
low = [0] * n
66
visited = [False] * n
@@ -12,7 +12,7 @@ def dfs(root, at, parent, out_edge_count):
1212
visited[at] = True
1313
low[at] = at
1414

15-
for to in l[at]:
15+
for to in graph[at]:
1616
if to == parent:
1717
pass
1818
elif not visited[to]:
@@ -41,7 +41,7 @@ def dfs(root, at, parent, out_edge_count):
4141

4242

4343
# Adjacency list of graph
44-
data = {
44+
graph = {
4545
0: [1, 2],
4646
1: [0, 2],
4747
2: [0, 1, 3, 5],
@@ -52,4 +52,4 @@ def dfs(root, at, parent, out_edge_count):
5252
7: [6, 8],
5353
8: [5, 7],
5454
}
55-
compute_ap(data)
55+
compute_ap(graph)

‎graphs/dinic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def depth_first_search(self, vertex, sink, flow):
3737
# Here we calculate the flow that reaches the sink
3838
def max_flow(self, source, sink):
3939
flow, self.q[0] = 0, source
40-
for l in range(31): # l = 30 maybe faster for random data
40+
for l in range(31): # l = 30 maybe faster for random data # noqa: E741
4141
while True:
4242
self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q)
4343
qi, qe, self.lvl[source] = 0, 1, 1

‎machine_learning/sequential_minimum_optimization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2):
309309
# calculate L and H which bound the new alpha2
310310
s = y1 * y2
311311
if s == -1:
312-
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1)
312+
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1) # noqa: E741
313313
else:
314-
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1)
314+
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1) # noqa: E741
315315
if l == h:
316316
return None, None
317317

‎maths/pi_generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def calculate_pi(limit: int) -> str:
4141
t = 1
4242
k = 1
4343
n = 3
44-
l = 3
44+
m = 3
4545

4646
decimal = limit
4747
counter = 0
@@ -65,11 +65,11 @@ def calculate_pi(limit: int) -> str:
6565
q *= 10
6666
r = nr
6767
else:
68-
nr = (2 * q + r) * l
69-
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
68+
nr = (2 * q + r) * m
69+
nn = (q * (7 * k) + 2 + (r * m)) // (t * m)
7070
q *= k
71-
t *= l
72-
l += 2
71+
t *= m
72+
m += 2
7373
k += 1
7474
n = nn
7575
r = nr

‎other/sdes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def function(expansion, s0, s1, key, message):
4444
right = message[4:]
4545
temp = apply_table(right, expansion)
4646
temp = xor(temp, key)
47-
l = apply_sbox(s0, temp[:4])
48-
r = apply_sbox(s1, temp[4:])
49-
l = "0" * (2 - len(l)) + l
50-
r = "0" * (2 - len(r)) + r
51-
temp = apply_table(l + r, p4_table)
47+
left_bin_str = apply_sbox(s0, temp[:4])
48+
right_bin_str = apply_sbox(s1, temp[4:])
49+
left_bin_str = "0" * (2 - len(left_bin_str)) + left_bin_str
50+
right_bin_str = "0" * (2 - len(right_bin_str)) + right_bin_str
51+
temp = apply_table(left_bin_str + right_bin_str, p4_table)
5252
temp = xor(left, temp)
5353
return temp + right
5454

‎project_euler/problem_011/sol2.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,47 @@ def solution():
3535
70600674
3636
"""
3737
with open(os.path.dirname(__file__) + "/grid.txt") as f:
38-
l = []
38+
grid = []
3939
for _ in range(20):
40-
l.append([int(x) for x in f.readline().split()])
40+
grid.append([int(x) for x in f.readline().split()])
4141

4242
maximum = 0
4343

4444
# right
4545
for i in range(20):
4646
for j in range(17):
47-
temp = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
47+
temp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]
4848
if temp > maximum:
4949
maximum = temp
5050

5151
# down
5252
for i in range(17):
5353
for j in range(20):
54-
temp = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
54+
temp = grid[i][j] * grid[i + 1][j] * grid[i + 2][j] * grid[i + 3][j]
5555
if temp > maximum:
5656
maximum = temp
5757

5858
# diagonal 1
5959
for i in range(17):
6060
for j in range(17):
61-
temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
61+
temp = (
62+
grid[i][j]
63+
* grid[i + 1][j + 1]
64+
* grid[i + 2][j + 2]
65+
* grid[i + 3][j + 3]
66+
)
6267
if temp > maximum:
6368
maximum = temp
6469

6570
# diagonal 2
6671
for i in range(17):
6772
for j in range(3, 20):
68-
temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
73+
temp = (
74+
grid[i][j]
75+
* grid[i + 1][j - 1]
76+
* grid[i + 2][j - 2]
77+
* grid[i + 3][j - 3]
78+
)
6979
if temp > maximum:
7080
maximum = temp
7181
return maximum

‎pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
lint.ignore = [ # `ruff rule S101` for a description of that rule
33
"B904", # Within an `except` clause, raise exceptions with `raise ... from err` -- FIX ME
44
"B905", # `zip()` without an explicit `strict=` parameter -- FIX ME
5-
"E741", # Ambiguous variable name 'l' -- FIX ME
65
"EM101", # Exception must not use a string literal, assign to variable first
76
"EXE001", # Shebang is present but file is not executable -- DO NOT FIX
87
"G004", # Logging statement uses f-string

‎strings/jaro_winkler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ def jaro_winkler(str1: str, str2: str) -> float:
2828
def get_matched_characters(_str1: str, _str2: str) -> str:
2929
matched = []
3030
limit = min(len(_str1), len(_str2)) // 2
31-
for i, l in enumerate(_str1):
31+
for i, char in enumerate(_str1):
3232
left = int(max(0, i - limit))
3333
right = int(min(i + limit + 1, len(_str2)))
34-
if l in _str2[left:right]:
35-
matched.append(l)
36-
_str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}"
34+
if char in _str2[left:right]:
35+
matched.append(char)
36+
_str2 = f"{_str2[0:_str2.index(char)]} {_str2[_str2.index(char) + 1:]}"
3737

3838
return "".join(matched)
3939

‎strings/manacher.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def palindromic_string(input_string: str) -> str:
99
1010
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
1111
positions are actual input characters.
12-
2. for each character in new_string it find corresponding length and store the
13-
length and l,r to store previously calculated info.(please look the explanation
14-
for details)
12+
2. for each character in new_string it find corresponding length and
13+
store the length and left,right to store previously calculated info.
14+
(please look the explanation for details)
1515
1616
3. return corresponding output_string by removing all "|"
1717
"""
@@ -29,15 +29,15 @@ def palindromic_string(input_string: str) -> str:
2929

3030
# we will store the starting and ending of previous furthest ending palindromic
3131
# substring
32-
l, r = 0, 0
32+
left, right = 0, 0
3333

3434
# length[i] shows the length of palindromic substring with center i
3535
length = [1 for i in range(len(new_input_string))]
3636

3737
# for each character in new_string find corresponding palindromic string
3838
start = 0
3939
for j in range(len(new_input_string)):
40-
k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
40+
k = 1 if j > right else min(length[left + right - j] // 2, right - j + 1)
4141
while (
4242
j - k >= 0
4343
and j + k < len(new_input_string)
@@ -47,11 +47,11 @@ def palindromic_string(input_string: str) -> str:
4747

4848
length[j] = 2 * k - 1
4949

50-
# does this string is ending after the previously explored end (that is r) ?
51-
# if yes the update the new r to the last index of this
52-
if j + k - 1 > r:
53-
l = j - k + 1
54-
r = j + k - 1
50+
# does this string is ending after the previously explored end (that is right) ?
51+
# if yes the update the new right to the last index of this
52+
if j + k - 1 > right:
53+
left = j - k + 1
54+
right = j + k - 1
5555

5656
# update max_length and start position
5757
if max_length < length[j]:
@@ -78,8 +78,9 @@ def palindromic_string(input_string: str) -> str:
7878
consider the string for which we are calculating the longest palindromic substring is
7979
shown above where ... are some characters in between and right now we are calculating
8080
the length of palindromic substring with center at a5 with following conditions :
81-
i) we have stored the length of palindromic substring which has center at a3 (starts at
82-
l ends at r) and it is the furthest ending till now, and it has ending after a6
81+
i) we have stored the length of palindromic substring which has center at a3
82+
(starts at left ends at right) and it is the furthest ending till now,
83+
and it has ending after a6
8384
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
8485
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
8586
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
@@ -98,11 +99,11 @@ def palindromic_string(input_string: str) -> str:
9899
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
99100
so finally ..
100101
101-
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)
102-
where a3 lies from l to r and we have to keep updating that
102+
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), right-a5)
103+
where a3 lies from left to right and we have to keep updating that
103104
104-
and if the a5 lies outside of l,r boundary we calculate length of palindrome with
105-
bruteforce and update l,r.
105+
and if the a5 lies outside of left,right boundary we calculate length of palindrome with
106+
bruteforce and update left,right.
106107
107108
it gives the linear time complexity just like z-function
108109
"""

0 commit comments

Comments
 (0)
Please sign in to comment.