Enable ruff E741 rule (#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>
This commit is contained in:
Maxim Smolskiy 2024-04-19 22:30:22 +03:00 committed by GitHub
parent 0a9a860eb1
commit a42eb35702
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 102 additions and 92 deletions

View File

@ -87,12 +87,12 @@ class SegmentTree(Generic[T]):
p = p // 2
self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1])
def query(self, l: int, r: int) -> T | None:
def query(self, left: int, right: int) -> T | None:
"""
Get range query value in log(N) time
:param l: left element index
:param r: right element index
:return: element combined in the range [l, r]
:param left: left element index
:param right: right element index
:return: element combined in the range [left, right]
>>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b)
>>> st.query(0, 2)
@ -104,15 +104,15 @@ class SegmentTree(Generic[T]):
>>> st.query(2, 3)
7
"""
l, r = l + self.N, r + self.N
left, right = left + self.N, right + self.N
res: T | None = None
while l <= r:
if l % 2 == 1:
res = self.st[l] if res is None else self.fn(res, self.st[l])
if r % 2 == 0:
res = self.st[r] if res is None else self.fn(res, self.st[r])
l, r = (l + 1) // 2, (r - 1) // 2
while left <= right:
if left % 2 == 1:
res = self.st[left] if res is None else self.fn(res, self.st[left])
if right % 2 == 0:
res = self.st[right] if res is None else self.fn(res, self.st[right])
left, right = (left + 1) // 2, (right - 1) // 2
return res

View File

@ -35,13 +35,13 @@ class SegmentTree:
"""
return idx * 2 + 1
def build(self, idx, l, r):
if l == r:
self.st[idx] = self.A[l]
def build(self, idx, left, right):
if left == right:
self.st[idx] = self.A[left]
else:
mid = (l + r) // 2
self.build(self.left(idx), l, mid)
self.build(self.right(idx), mid + 1, r)
mid = (left + right) // 2
self.build(self.left(idx), left, mid)
self.build(self.right(idx), mid + 1, right)
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
def update(self, a, b, val):
@ -56,18 +56,18 @@ class SegmentTree:
"""
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
def update_recursive(self, idx, l, r, a, b, val):
def update_recursive(self, idx, left, right, a, b, val):
"""
update(1, 1, N, a, b, v) for update val v to [a,b]
"""
if r < a or l > b:
if right < a or left > b:
return True
if l == r:
if left == right:
self.st[idx] = val
return True
mid = (l + r) // 2
self.update_recursive(self.left(idx), l, mid, a, b, val)
self.update_recursive(self.right(idx), mid + 1, r, a, b, val)
mid = (left + right) // 2
self.update_recursive(self.left(idx), left, mid, a, b, val)
self.update_recursive(self.right(idx), mid + 1, right, a, b, val)
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
return True
@ -83,17 +83,17 @@ class SegmentTree:
"""
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
def query_recursive(self, idx, l, r, a, b):
def query_recursive(self, idx, left, right, a, b):
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
if r < a or l > b:
if right < a or left > b:
return -math.inf
if l >= a and r <= b:
if left >= a and right <= b:
return self.st[idx]
mid = (l + r) // 2
q1 = self.query_recursive(self.left(idx), l, mid, a, b)
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
mid = (left + right) // 2
q1 = self.query_recursive(self.left(idx), left, mid, a, b)
q2 = self.query_recursive(self.right(idx), mid + 1, right, a, b)
return max(q1, q2)
def show_data(self):

View File

@ -66,14 +66,14 @@ class MinHeap:
# this is min-heapify method
def sift_down(self, idx, array):
while True:
l = self.get_left_child_idx(idx)
r = self.get_right_child_idx(idx)
left = self.get_left_child_idx(idx)
right = self.get_right_child_idx(idx)
smallest = idx
if l < len(array) and array[l] < array[idx]:
smallest = l
if r < len(array) and array[r] < array[smallest]:
smallest = r
if left < len(array) and array[left] < array[idx]:
smallest = left
if right < len(array) and array[right] < array[smallest]:
smallest = right
if smallest != idx:
array[idx], array[smallest] = array[smallest], array[idx]

View File

@ -38,30 +38,30 @@ def longest_common_subsequence(x: str, y: str):
n = len(y)
# declaring the array for storing the dp values
l = [[0] * (n + 1) for _ in range(m + 1)]
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
match = 1 if x[i - 1] == y[j - 1] else 0
l[i][j] = max(l[i - 1][j], l[i][j - 1], l[i - 1][j - 1] + match)
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + match)
seq = ""
i, j = m, n
while i > 0 and j > 0:
match = 1 if x[i - 1] == y[j - 1] else 0
if l[i][j] == l[i - 1][j - 1] + match:
if dp[i][j] == dp[i - 1][j - 1] + match:
if match == 1:
seq = x[i - 1] + seq
i -= 1
j -= 1
elif l[i][j] == l[i - 1][j]:
elif dp[i][j] == dp[i - 1][j]:
i -= 1
else:
j -= 1
return l[m][n], seq
return dp[m][n], seq
if __name__ == "__main__":

View File

@ -7,14 +7,14 @@
from __future__ import annotations
def ceil_index(v, l, r, key):
while r - l > 1:
m = (l + r) // 2
if v[m] >= key:
r = m
def ceil_index(v, left, right, key):
while right - left > 1:
middle = (left + right) // 2
if v[middle] >= key:
right = middle
else:
l = m
return r
left = middle
return right
def longest_increasing_subsequence_length(v: list[int]) -> int:

View File

@ -1,6 +1,6 @@
# Finding Articulation Points in Undirected Graph
def compute_ap(l):
n = len(l)
def compute_ap(graph):
n = len(graph)
out_edge_count = 0
low = [0] * n
visited = [False] * n
@ -12,7 +12,7 @@ def compute_ap(l):
visited[at] = True
low[at] = at
for to in l[at]:
for to in graph[at]:
if to == parent:
pass
elif not visited[to]:
@ -41,7 +41,7 @@ def compute_ap(l):
# Adjacency list of graph
data = {
graph = {
0: [1, 2],
1: [0, 2],
2: [0, 1, 3, 5],
@ -52,4 +52,4 @@ data = {
7: [6, 8],
8: [5, 7],
}
compute_ap(data)
compute_ap(graph)

View File

@ -37,7 +37,7 @@ class Dinic:
# Here we calculate the flow that reaches the sink
def max_flow(self, source, sink):
flow, self.q[0] = 0, source
for l in range(31): # l = 30 maybe faster for random data
for l in range(31): # l = 30 maybe faster for random data # noqa: E741
while True:
self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q)
qi, qe, self.lvl[source] = 0, 1, 1

View File

@ -309,9 +309,9 @@ class SmoSVM:
# calculate L and H which bound the new alpha2
s = y1 * y2
if s == -1:
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1)
l, h = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1) # noqa: E741
else:
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1)
l, h = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1) # noqa: E741
if l == h:
return None, None

View File

@ -41,7 +41,7 @@ def calculate_pi(limit: int) -> str:
t = 1
k = 1
n = 3
l = 3
m = 3
decimal = limit
counter = 0
@ -65,11 +65,11 @@ def calculate_pi(limit: int) -> str:
q *= 10
r = nr
else:
nr = (2 * q + r) * l
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
nr = (2 * q + r) * m
nn = (q * (7 * k) + 2 + (r * m)) // (t * m)
q *= k
t *= l
l += 2
t *= m
m += 2
k += 1
n = nn
r = nr

View File

@ -44,11 +44,11 @@ def function(expansion, s0, s1, key, message):
right = message[4:]
temp = apply_table(right, expansion)
temp = xor(temp, key)
l = apply_sbox(s0, temp[:4])
r = apply_sbox(s1, temp[4:])
l = "0" * (2 - len(l)) + l
r = "0" * (2 - len(r)) + r
temp = apply_table(l + r, p4_table)
left_bin_str = apply_sbox(s0, temp[:4])
right_bin_str = apply_sbox(s1, temp[4:])
left_bin_str = "0" * (2 - len(left_bin_str)) + left_bin_str
right_bin_str = "0" * (2 - len(right_bin_str)) + right_bin_str
temp = apply_table(left_bin_str + right_bin_str, p4_table)
temp = xor(left, temp)
return temp + right

View File

@ -35,37 +35,47 @@ def solution():
70600674
"""
with open(os.path.dirname(__file__) + "/grid.txt") as f:
l = []
grid = []
for _ in range(20):
l.append([int(x) for x in f.readline().split()])
grid.append([int(x) for x in f.readline().split()])
maximum = 0
# right
for i in range(20):
for j in range(17):
temp = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
temp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]
if temp > maximum:
maximum = temp
# down
for i in range(17):
for j in range(20):
temp = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
temp = grid[i][j] * grid[i + 1][j] * grid[i + 2][j] * grid[i + 3][j]
if temp > maximum:
maximum = temp
# diagonal 1
for i in range(17):
for j in range(17):
temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
temp = (
grid[i][j]
* grid[i + 1][j + 1]
* grid[i + 2][j + 2]
* grid[i + 3][j + 3]
)
if temp > maximum:
maximum = temp
# diagonal 2
for i in range(17):
for j in range(3, 20):
temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
temp = (
grid[i][j]
* grid[i + 1][j - 1]
* grid[i + 2][j - 2]
* grid[i + 3][j - 3]
)
if temp > maximum:
maximum = temp
return maximum

View File

@ -2,7 +2,6 @@
lint.ignore = [ # `ruff rule S101` for a description of that rule
"B904", # Within an `except` clause, raise exceptions with `raise ... from err` -- FIX ME
"B905", # `zip()` without an explicit `strict=` parameter -- FIX ME
"E741", # Ambiguous variable name 'l' -- FIX ME
"EM101", # Exception must not use a string literal, assign to variable first
"EXE001", # Shebang is present but file is not executable -- DO NOT FIX
"G004", # Logging statement uses f-string

View File

@ -28,12 +28,12 @@ def jaro_winkler(str1: str, str2: str) -> float:
def get_matched_characters(_str1: str, _str2: str) -> str:
matched = []
limit = min(len(_str1), len(_str2)) // 2
for i, l in enumerate(_str1):
for i, char in enumerate(_str1):
left = int(max(0, i - limit))
right = int(min(i + limit + 1, len(_str2)))
if l in _str2[left:right]:
matched.append(l)
_str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}"
if char in _str2[left:right]:
matched.append(char)
_str2 = f"{_str2[0:_str2.index(char)]} {_str2[_str2.index(char) + 1:]}"
return "".join(matched)

View File

@ -9,9 +9,9 @@ def palindromic_string(input_string: str) -> str:
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
positions are actual input characters.
2. for each character in new_string it find corresponding length and store the
length and l,r to store previously calculated info.(please look the explanation
for details)
2. for each character in new_string it find corresponding length and
store the length and left,right to store previously calculated info.
(please look the explanation for details)
3. return corresponding output_string by removing all "|"
"""
@ -29,7 +29,7 @@ def palindromic_string(input_string: str) -> str:
# we will store the starting and ending of previous furthest ending palindromic
# substring
l, r = 0, 0
left, right = 0, 0
# length[i] shows the length of palindromic substring with center i
length = [1 for i in range(len(new_input_string))]
@ -37,7 +37,7 @@ def palindromic_string(input_string: str) -> str:
# for each character in new_string find corresponding palindromic string
start = 0
for j in range(len(new_input_string)):
k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
k = 1 if j > right else min(length[left + right - j] // 2, right - j + 1)
while (
j - k >= 0
and j + k < len(new_input_string)
@ -47,11 +47,11 @@ def palindromic_string(input_string: str) -> str:
length[j] = 2 * k - 1
# does this string is ending after the previously explored end (that is r) ?
# if yes the update the new r to the last index of this
if j + k - 1 > r:
l = j - k + 1
r = j + k - 1
# does this string is ending after the previously explored end (that is right) ?
# if yes the update the new right to the last index of this
if j + k - 1 > right:
left = j - k + 1
right = j + k - 1
# update max_length and start position
if max_length < length[j]:
@ -78,8 +78,9 @@ if __name__ == "__main__":
consider the string for which we are calculating the longest palindromic substring is
shown above where ... are some characters in between and right now we are calculating
the length of palindromic substring with center at a5 with following conditions :
i) we have stored the length of palindromic substring which has center at a3 (starts at
l ends at r) and it is the furthest ending till now, and it has ending after a6
i) we have stored the length of palindromic substring which has center at a3
(starts at left ends at right) and it is the furthest ending till now,
and it has ending after a6
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
@ -98,11 +99,11 @@ so we can say that palindrome at center a5 is at least as long as palindrome at
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
so finally ..
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)
where a3 lies from l to r and we have to keep updating that
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), right-a5)
where a3 lies from left to right and we have to keep updating that
and if the a5 lies outside of l,r boundary we calculate length of palindrome with
bruteforce and update l,r.
and if the a5 lies outside of left,right boundary we calculate length of palindrome with
bruteforce and update left,right.
it gives the linear time complexity just like z-function
"""