Skip to content

Commit d5a9f64

Browse files
CaedenPHcclausspre-commit-ci[bot]dhruvmanila
authoredOct 13, 2022
Add flake8-builtins to pre-commit and fix errors (TheAlgorithms#7105)
Ignore `A003` Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent e661b98 commit d5a9f64

31 files changed

+113
-106
lines changed
 

‎.flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
extend-ignore =
3+
A003 # Class attribute is shadowing a python builtin

‎.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ repos:
4040
- --ignore=E203,W503
4141
- --max-complexity=25
4242
- --max-line-length=88
43-
additional_dependencies: [pep8-naming]
43+
additional_dependencies: [flake8-builtins, pep8-naming]
4444

4545
- repo: https://github.com/pre-commit/mirrors-mypy
4646
rev: v0.982

‎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+
total = 0
3737
for col in range(row + 1, columns):
38-
sum += coefficients[row, col] * x[col]
38+
total += coefficients[row, col] * x[col]
3939

40-
x[row, 0] = (vector[row] - sum) / coefficients[row, row]
40+
x[row, 0] = (vector[row] - total) / 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+
total = 0
151151
for j in range(0, cols - 1):
152152
if i == j:
153153
continue
154154
else:
155-
sum += table[i][j]
155+
total += table[i][j]
156156

157-
if table[i][i] <= sum:
157+
if table[i][i] <= total:
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/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/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

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Deque:
1515
----------
1616
append(val: Any) -> None
1717
appendleft(val: Any) -> None
18-
extend(iter: Iterable) -> None
19-
extendleft(iter: Iterable) -> None
18+
extend(iterable: Iterable) -> None
19+
extendleft(iterable: Iterable) -> None
2020
pop() -> Any
2121
popleft() -> Any
2222
Observers
@@ -179,9 +179,9 @@ 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
"""
184-
Appends every value of iter to the end of the deque.
184+
Appends every value of iterable to the end of the deque.
185185
Time complexity: O(n)
186186
>>> our_deque_1 = Deque([1, 2, 3])
187187
>>> our_deque_1.extend([4, 5])
@@ -205,12 +205,12 @@ 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
"""
213-
Appends every value of iter to the beginning of the deque.
213+
Appends every value of iterable to the beginning of the deque.
214214
Time complexity: O(n)
215215
>>> our_deque_1 = Deque([1, 2, 3])
216216
>>> our_deque_1.extendleft([0, -1])
@@ -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_element: float = -1
2121
for j in range(i + 1, arr_size):
2222
if arr[i] < arr[j]:
23-
next = arr[j]
23+
next_element = arr[j]
2424
break
25-
result.append(next)
25+
result.append(next_element)
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_item: float = -1
4040
for inner in arr[i + 1 :]:
4141
if outer < inner:
42-
next = inner
42+
next_item = inner
4343
break
44-
result.append(next)
44+
result.append(next_item)
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_value = np.max([np.max(self.red), np.max(self.green), np.max(self.blue)])
501+
min_value = np.min([np.min(self.red), np.min(self.green), np.min(self.blue)])
502+
return (max_value - min_value) / max_value
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_value = 5381
3333
for x in s:
34-
hash = ((hash << 5) + hash) + ord(x)
35-
return hash & 0xFFFFFFFF
34+
hash_value = ((hash_value << 5) + hash_value) + ord(x)
35+
return hash_value & 0xFFFFFFFF

‎hashes/sdbm.py

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

‎maths/armstrong_numbers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def armstrong_number(n: int) -> bool:
2525
return False
2626

2727
# Initialization of sum and number of digits.
28-
sum = 0
28+
total = 0
2929
number_of_digits = 0
3030
temp = n
3131
# Calculation of digits of the number
@@ -36,9 +36,9 @@ def armstrong_number(n: int) -> bool:
3636
temp = n
3737
while temp > 0:
3838
rem = temp % 10
39-
sum += rem**number_of_digits
39+
total += rem**number_of_digits
4040
temp //= 10
41-
return n == sum
41+
return n == total
4242

4343

4444
def pluperfect_number(n: int) -> bool:
@@ -55,17 +55,17 @@ def pluperfect_number(n: int) -> bool:
5555
# Init a "histogram" of the digits
5656
digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
5757
digit_total = 0
58-
sum = 0
58+
total = 0
5959
temp = n
6060
while temp > 0:
6161
temp, rem = divmod(temp, 10)
6262
digit_histogram[rem] += 1
6363
digit_total += 1
6464

6565
for (cnt, i) in zip(digit_histogram, range(len(digit_histogram))):
66-
sum += cnt * i**digit_total
66+
total += cnt * i**digit_total
6767

68-
return n == sum
68+
return n == total
6969

7070

7171
def narcissistic_number(n: int) -> bool:

0 commit comments

Comments
 (0)
Please sign in to comment.