Skip to content

Enable ruff E741 rule #11370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions data_structures/binary_tree/non_recursive_segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def update(self, p: int, v: T) -> None:
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)
Expand All @@ -104,15 +104,15 @@ def query(self, l: int, r: int) -> T | None:
>>> 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


Expand Down
36 changes: 18 additions & 18 deletions data_structures/binary_tree/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def right(self, idx):
"""
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):
Expand All @@ -56,18 +56,18 @@ def update(self, a, b, val):
"""
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

Expand All @@ -83,17 +83,17 @@ def query(self, a, b):
"""
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):
Expand Down
12 changes: 6 additions & 6 deletions data_structures/heap/min_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def build_heap(self, array):
# 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]
Expand Down
10 changes: 5 additions & 5 deletions dynamic_programming/longest_common_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
14 changes: 7 additions & 7 deletions dynamic_programming/longest_increasing_subsequence_o_nlogn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions graphs/articulation_points.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +12,7 @@ def dfs(root, at, parent, out_edge_count):
visited[at] = True
low[at] = at

for to in l[at]:
for to in graph[at]:
if to == parent:
pass
elif not visited[to]:
Expand Down Expand Up @@ -41,7 +41,7 @@ def dfs(root, at, parent, out_edge_count):


# Adjacency list of graph
data = {
graph = {
0: [1, 2],
1: [0, 2],
2: [0, 1, 3, 5],
Expand All @@ -52,4 +52,4 @@ def dfs(root, at, parent, out_edge_count):
7: [6, 8],
8: [5, 7],
}
compute_ap(data)
compute_ap(graph)
2 changes: 1 addition & 1 deletion graphs/dinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def depth_first_search(self, vertex, sink, flow):
# 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
Expand Down
4 changes: 2 additions & 2 deletions machine_learning/sequential_minimum_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2):
# 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

Expand Down
10 changes: 5 additions & 5 deletions maths/pi_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def calculate_pi(limit: int) -> str:
t = 1
k = 1
n = 3
l = 3
m = 3

decimal = limit
counter = 0
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions other/sdes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions project_euler/problem_011/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions strings/jaro_winkler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading