Skip to content

Commit 771e65b

Browse files
committed
refactor: Fix flake8-builtins (#7104)
1 parent 6796e27 commit 771e65b

35 files changed

+119
-113
lines changed

arithmetic_analysis/gaussian_elimination.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def retroactive_resolution(
3333

3434
x: NDArray[float64] = np.zeros((rows, 1), dtype=float)
3535
for row in reversed(range(rows)):
36-
sum = 0
36+
count = 0
3737
for col in range(row + 1, columns):
38-
sum += coefficients[row, col] * x[col]
38+
count += coefficients[row, col] * x[col]
3939

40-
x[row, 0] = (vector[row] - sum) / coefficients[row, row]
40+
x[row, 0] = (vector[row] - count) / coefficients[row, row]
4141

4242
return x
4343

arithmetic_analysis/jacobi_iteration_method.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ def strictly_diagonally_dominant(table: NDArray[float64]) -> bool:
147147
is_diagonally_dominant = True
148148

149149
for i in range(0, rows):
150-
sum = 0
150+
count = 0
151151
for j in range(0, cols - 1):
152152
if i == j:
153153
continue
154154
else:
155-
sum += table[i][j]
155+
count += table[i][j]
156156

157-
if table[i][i] <= sum:
157+
if table[i][i] <= count:
158158
raise ValueError("Coefficient matrix is not strictly diagonally dominant")
159159

160160
return is_diagonally_dominant

audio_filters/show_response.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_bounds(
3434
return lowest, highest
3535

3636

37-
def show_frequency_response(filter: FilterType, samplerate: int) -> None:
37+
def show_frequency_response(filter_type: FilterType, samplerate: int) -> None:
3838
"""
3939
Show frequency response of a filter
4040
@@ -45,7 +45,7 @@ def show_frequency_response(filter: FilterType, samplerate: int) -> None:
4545

4646
size = 512
4747
inputs = [1] + [0] * (size - 1)
48-
outputs = [filter.process(item) for item in inputs]
48+
outputs = [filter_type.process(item) for item in inputs]
4949

5050
filler = [0] * (samplerate - size) # zero-padding
5151
outputs += filler
@@ -66,7 +66,7 @@ def show_frequency_response(filter: FilterType, samplerate: int) -> None:
6666
plt.show()
6767

6868

69-
def show_phase_response(filter: FilterType, samplerate: int) -> None:
69+
def show_phase_response(filter_type: FilterType, samplerate: int) -> None:
7070
"""
7171
Show phase response of a filter
7272
@@ -77,7 +77,7 @@ def show_phase_response(filter: FilterType, samplerate: int) -> None:
7777

7878
size = 512
7979
inputs = [1] + [0] * (size - 1)
80-
outputs = [filter.process(item) for item in inputs]
80+
outputs = [filter_type.process(item) for item in inputs]
8181

8282
filler = [0] * (samplerate - size) # zero-padding
8383
outputs += filler

backtracking/hamiltonian_cycle.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ 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 in range(0, len(graph)):
99-
if valid_connection(graph, next, curr_ind, path):
98+
for next_ver in range(0, len(graph)):
99+
if valid_connection(graph, next_ver, curr_ind, path):
100100
# Insert current vertex into path as next transition
101-
path[curr_ind] = next
101+
path[curr_ind] = next_ver
102102
# Validate created path
103103
if util_hamilton_cycle(graph, path, curr_ind + 1):
104104
return True

data_structures/binary_tree/avl_tree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def pop(self) -> Any:
3333
def count(self) -> int:
3434
return self.tail - self.head
3535

36-
def print(self) -> None:
36+
def print_queue(self) -> None:
3737
print(self.data)
3838
print("**************")
3939
print(self.data[self.head : self.tail])

data_structures/binary_tree/fenwick_tree.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def init(self, arr: list[int]) -> None:
4646
self.size = len(arr)
4747
self.tree = deepcopy(arr)
4848
for i in range(1, self.size):
49-
j = self.next(i)
49+
j = self.next_index(i)
5050
if j < self.size:
5151
self.tree[j] += self.tree[i]
5252

@@ -64,13 +64,13 @@ def get_array(self) -> list[int]:
6464
"""
6565
arr = self.tree[:]
6666
for i in range(self.size - 1, 0, -1):
67-
j = self.next(i)
67+
j = self.next_index(i)
6868
if j < self.size:
6969
arr[j] -= arr[i]
7070
return arr
7171

7272
@staticmethod
73-
def next(index: int) -> int:
73+
def next_index(index: int) -> int:
7474
return index + (index & (-index))
7575

7676
@staticmethod
@@ -102,7 +102,7 @@ def add(self, index: int, value: int) -> None:
102102
return
103103
while index < self.size:
104104
self.tree[index] += value
105-
index = self.next(index)
105+
index = self.next_index(index)
106106

107107
def update(self, index: int, value: int) -> None:
108108
"""

data_structures/heap/heap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def build_max_heap(self, collection: Iterable[float]) -> None:
8888
for i in range(self.heap_size // 2 - 1, -1, -1):
8989
self.max_heapify(i)
9090

91-
def max(self) -> float:
91+
def get_max(self) -> float:
9292
"""return the max in the heap"""
9393
if self.heap_size >= 1:
9494
return self.h[0]

data_structures/linked_list/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class Node:
14-
def __init__(self, item: Any, next: Any) -> None:
14+
def __init__(self, item: Any, next: Any) -> None: # noqa: A002
1515
self.item = item
1616
self.next = next
1717

data_structures/linked_list/merge_two_lists.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@dataclass
1414
class Node:
1515
data: int
16-
next: Node | None
16+
next: Node | None # noqa: A003
1717

1818

1919
class SortedLinkedList:

data_structures/linked_list/singly_linked_list.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_singly_linked_list_2() -> None:
392392
This section of the test used varying data types for input.
393393
>>> test_singly_linked_list_2()
394394
"""
395-
input = [
395+
test_input = [
396396
-9,
397397
100,
398398
Node(77345112),
@@ -410,7 +410,7 @@ def test_singly_linked_list_2() -> None:
410410
]
411411
linked_list = LinkedList()
412412

413-
for i in input:
413+
for i in test_input:
414414
linked_list.insert_tail(i)
415415

416416
# Check if it's empty or not

data_structures/queue/double_ended_queue.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class _Node:
4242
"""
4343

4444
val: Any = None
45-
next: Deque._Node | None = None
45+
next: Deque._Node | None = None # noqa: A003
4646
prev: Deque._Node | None = None
4747

4848
class _Iterator:
@@ -179,7 +179,7 @@ def appendleft(self, val: Any) -> None:
179179
# make sure there were no errors
180180
assert not self.is_empty(), "Error on appending value."
181181

182-
def extend(self, iter: Iterable[Any]) -> None:
182+
def extend(self, iterable: Iterable[Any]) -> None:
183183
"""
184184
Appends every value of iter to the end of the deque.
185185
Time complexity: O(n)
@@ -205,10 +205,10 @@ def extend(self, iter: Iterable[Any]) -> None:
205205
>>> list(our_deque_2) == list(deque_collections_2)
206206
True
207207
"""
208-
for val in iter:
208+
for val in iterable:
209209
self.append(val)
210210

211-
def extendleft(self, iter: Iterable[Any]) -> None:
211+
def extendleft(self, iterable: Iterable[Any]) -> None:
212212
"""
213213
Appends every value of iter to the beginning of the deque.
214214
Time complexity: O(n)
@@ -234,7 +234,7 @@ def extendleft(self, iter: Iterable[Any]) -> None:
234234
>>> list(our_deque_2) == list(deque_collections_2)
235235
True
236236
"""
237-
for val in iter:
237+
for val in iterable:
238238
self.appendleft(val)
239239

240240
def pop(self) -> Any:

data_structures/stacks/next_greater_element.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def next_greatest_element_slow(arr: list[float]) -> list[float]:
1717
arr_size = len(arr)
1818

1919
for i in range(arr_size):
20-
next: float = -1
20+
_next: float = -1
2121
for j in range(i + 1, arr_size):
2222
if arr[i] < arr[j]:
23-
next = arr[j]
23+
_next = arr[j]
2424
break
25-
result.append(next)
25+
result.append(_next)
2626
return result
2727

2828

@@ -36,12 +36,12 @@ def next_greatest_element_fast(arr: list[float]) -> list[float]:
3636
"""
3737
result = []
3838
for i, outer in enumerate(arr):
39-
next: float = -1
39+
_next: float = -1
4040
for inner in arr[i + 1 :]:
4141
if outer < inner:
42-
next = inner
42+
_next = inner
4343
break
44-
result.append(next)
44+
result.append(_next)
4545
return result
4646

4747

digital_image_processing/index_calculation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@ def s(self):
497497
https://www.indexdatabase.de/db/i-single.php?id=77
498498
:return: index
499499
"""
500-
max = np.max([np.max(self.red), np.max(self.green), np.max(self.blue)])
501-
min = np.min([np.min(self.red), np.min(self.green), np.min(self.blue)])
502-
return (max - min) / max
500+
_max = np.max([np.max(self.red), np.max(self.green), np.max(self.blue)])
501+
_min = np.min([np.min(self.red), np.min(self.green), np.min(self.blue)])
502+
return (_max - _min) / _max
503503

504504
def _if(self):
505505
"""

dynamic_programming/optimal_binary_search_tree.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def find_optimal_binary_search_tree(nodes):
104104
dp = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)]
105105
# sum[i][j] stores the sum of key frequencies between i and j inclusive in nodes
106106
# array
107-
sum = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)]
107+
total = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)]
108108
# stores tree roots that will be used later for constructing binary search tree
109109
root = [[i if i == j else 0 for j in range(n)] for i in range(n)]
110110

@@ -113,14 +113,14 @@ def find_optimal_binary_search_tree(nodes):
113113
j = i + interval_length - 1
114114

115115
dp[i][j] = sys.maxsize # set the value to "infinity"
116-
sum[i][j] = sum[i][j - 1] + freqs[j]
116+
total[i][j] = total[i][j - 1] + freqs[j]
117117

118118
# Apply Knuth's optimization
119119
# Loop without optimization: for r in range(i, j + 1):
120120
for r in range(root[i][j - 1], root[i + 1][j] + 1): # r is a temporal root
121121
left = dp[i][r - 1] if r != i else 0 # optimal cost for left subtree
122122
right = dp[r + 1][j] if r != j else 0 # optimal cost for right subtree
123-
cost = left + sum[i][j] + right
123+
cost = left + total[i][j] + right
124124

125125
if dp[i][j] > cost:
126126
dp[i][j] = cost

graphs/a_star.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def search(
4040
else: # to choose the least costliest action so as to move closer to the goal
4141
cell.sort()
4242
cell.reverse()
43-
next = cell.pop()
44-
x = next[2]
45-
y = next[3]
46-
g = next[1]
43+
next_cell = cell.pop()
44+
x = next_cell[2]
45+
y = next_cell[3]
46+
g = next_cell[1]
4747

4848
if x == goal[0] and y == goal[1]:
4949
found = True

graphs/dijkstra.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def dijkstra(graph, start, end):
5656
for v, c in graph[u]:
5757
if v in visited:
5858
continue
59-
next = cost + c
60-
heapq.heappush(heap, (next, v))
59+
next_item = cost + c
60+
heapq.heappush(heap, (next_item, v))
6161
return -1
6262

6363

graphs/finding_bridges.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ def compute_bridges(graph: dict[int, list[int]]) -> list[tuple[int, int]]:
7272
[]
7373
"""
7474

75-
id = 0
75+
id_ = 0
7676
n = len(graph) # No of vertices in graph
7777
low = [0] * n
7878
visited = [False] * n
7979

80-
def dfs(at, parent, bridges, id):
80+
def dfs(at, parent, bridges, id_):
8181
visited[at] = True
82-
low[at] = id
83-
id += 1
82+
low[at] = id_
83+
id_ += 1
8484
for to in graph[at]:
8585
if to == parent:
8686
pass
8787
elif not visited[to]:
88-
dfs(to, at, bridges, id)
88+
dfs(to, at, bridges, id_)
8989
low[at] = min(low[at], low[to])
90-
if id <= low[to]:
90+
if id_ <= low[to]:
9191
bridges.append((at, to) if at < to else (to, at))
9292
else:
9393
# This edge is a back edge and cannot be a bridge
@@ -96,7 +96,7 @@ def dfs(at, parent, bridges, id):
9696
bridges: list[tuple[int, int]] = []
9797
for i in range(n):
9898
if not visited[i]:
99-
dfs(i, -1, bridges, id)
99+
dfs(i, -1, bridges, id_)
100100
return bridges
101101

102102

graphs/prim.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
class Vertex:
1414
"""Class Vertex."""
1515

16-
def __init__(self, id):
16+
def __init__(self, _id):
1717
"""
1818
Arguments:
1919
id - input an id to identify the vertex
2020
Attributes:
2121
neighbors - a list of the vertices it is linked to
2222
edges - a dict to store the edges's weight
2323
"""
24-
self.id = str(id)
24+
self.id = str(_id)
2525
self.key = None
2626
self.pi = None
2727
self.neighbors = []

hashes/djb2.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def djb2(s: str) -> int:
2929
>>> djb2('scramble bits')
3030
1609059040
3131
"""
32-
hash = 5381
32+
_hash = 5381
3333
for x in s:
34-
hash = ((hash << 5) + hash) + ord(x)
35-
return hash & 0xFFFFFFFF
34+
_hash = ((_hash << 5) + _hash) + ord(x)
35+
return _hash & 0xFFFFFFFF

hashes/sdbm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def sdbm(plain_text: str) -> int:
3131
>>> sdbm('scramble bits')
3232
730247649148944819640658295400555317318720608290373040936089
3333
"""
34-
hash = 0
34+
_hash = 0
3535
for plain_chr in plain_text:
36-
hash = ord(plain_chr) + (hash << 6) + (hash << 16) - hash
37-
return hash
36+
_hash = ord(plain_chr) + (_hash << 6) + (_hash << 16) - _hash
37+
return _hash

linear_algebra/src/lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __mul__(self, other: float | Vector) -> float | Vector:
119119
else: # error case
120120
raise Exception("invalid operand!")
121121

122-
def set(self, components: Collection[float]) -> None:
122+
def set_components(self, components: Collection[float]) -> None:
123123
"""
124124
input: new components
125125
changes the components of the vector.

0 commit comments

Comments
 (0)