Skip to content

Commit a652905

Browse files
authored
Add Flake8 comprehensions to pre-commit (TheAlgorithms#7235)
* ci(pre-commit): Add ``flake8-comprehensions`` to ``pre-commit`` (TheAlgorithms#7233) * refactor: Fix ``flake8-comprehensions`` errors * fix: Replace `map` with generator (TheAlgorithms#7233) * fix: Cast `range` objects to `list`
1 parent 98a4c24 commit a652905

File tree

20 files changed

+36
-37
lines changed

20 files changed

+36
-37
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ repos:
3939
additional_dependencies:
4040
- flake8-bugbear
4141
- flake8-builtins
42+
- flake8-comprehensions
4243
- pep8-naming
4344

4445
- repo: https://github.com/pre-commit/mirrors-mypy

ciphers/onepad_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def decrypt(cipher: list[int], key: list[int]) -> str:
2222
for i in range(len(key)):
2323
p = int((cipher[i] - (key[i]) ** 2) / key[i])
2424
plain.append(chr(p))
25-
return "".join([i for i in plain])
25+
return "".join(plain)
2626

2727

2828
if __name__ == "__main__":

ciphers/rail_fence_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def decrypt(input_string: str, key: int) -> str:
7272
counter = 0
7373
for row in temp_grid: # fills in the characters
7474
splice = input_string[counter : counter + len(row)]
75-
grid.append([character for character in splice])
75+
grid.append(list(splice))
7676
counter += len(row)
7777

7878
output_string = "" # reads as zigzag

data_structures/hashing/hash_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def hash_function(self, key):
3434
def _step_by_step(self, step_ord):
3535

3636
print(f"step {step_ord}")
37-
print([i for i in range(len(self.values))])
37+
print(list(range(len(self.values))))
3838
print(self.values)
3939

4040
def bulk_insert(self, values):

data_structures/linked_list/merge_two_lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Node:
1919
class SortedLinkedList:
2020
def __init__(self, ints: Iterable[int]) -> None:
2121
self.head: Node | None = None
22-
for i in reversed(sorted(ints)):
22+
for i in sorted(ints, reverse=True):
2323
self.head = Node(i, self.head)
2424

2525
def __iter__(self) -> Iterator[int]:

dynamic_programming/fractional_knapsack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def frac_knapsack(vl, wt, w, n):
88
240.0
99
"""
1010

11-
r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True))
11+
r = sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True)
1212
vl, wt = [i[0] for i in r], [i[1] for i in r]
1313
acc = list(accumulate(wt))
1414
k = bisect(acc, w)

graphs/bellman_ford.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def bellman_ford(
5858
V = int(input("Enter number of vertices: ").strip())
5959
E = int(input("Enter number of edges: ").strip())
6060

61-
graph: list[dict[str, int]] = [dict() for j in range(E)]
61+
graph: list[dict[str, int]] = [{} for _ in range(E)]
6262

6363
for i in range(E):
6464
print("Edge ", i + 1)

graphs/frequent_pattern_graph_miner.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ def construct_graph(cluster, nodes):
155155
cluster[max(cluster.keys()) + 1] = "Header"
156156
graph = {}
157157
for i in x:
158-
if tuple(["Header"]) in graph:
159-
graph[tuple(["Header"])].append(x[i])
158+
if (["Header"],) in graph:
159+
graph[(["Header"],)].append(x[i])
160160
else:
161-
graph[tuple(["Header"])] = [x[i]]
161+
graph[(["Header"],)] = [x[i]]
162162
for i in x:
163-
graph[tuple(x[i])] = [["Header"]]
163+
graph[(x[i],)] = [["Header"]]
164164
i = 1
165165
while i < max(cluster) - 1:
166166
create_edge(nodes, graph, cluster, i)
@@ -186,7 +186,7 @@ def find_freq_subgraph_given_support(s, cluster, graph):
186186
"""
187187
k = int(s / 100 * (len(cluster) - 1))
188188
for i in cluster[k].keys():
189-
my_dfs(graph, tuple(cluster[k][i]), tuple(["Header"]))
189+
my_dfs(graph, tuple(cluster[k][i]), (["Header"],))
190190

191191

192192
def freq_subgraphs_edge_list(paths):

hashes/enigma_machine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
alphabets = [chr(i) for i in range(32, 126)]
2-
gear_one = [i for i in range(len(alphabets))]
3-
gear_two = [i for i in range(len(alphabets))]
4-
gear_three = [i for i in range(len(alphabets))]
5-
reflector = [i for i in reversed(range(len(alphabets)))]
2+
gear_one = list(range(len(alphabets)))
3+
gear_two = list(range(len(alphabets)))
4+
gear_three = list(range(len(alphabets)))
5+
reflector = list(reversed(range(len(alphabets))))
66
code = []
77
gear_one_pos = gear_two_pos = gear_three_pos = 0
88

maths/primelib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def sieve_er(n):
8989
assert isinstance(n, int) and (n > 2), "'N' must been an int and > 2"
9090

9191
# beginList: contains all natural numbers from 2 up to N
92-
begin_list = [x for x in range(2, n + 1)]
92+
begin_list = list(range(2, n + 1))
9393

9494
ans = [] # this list will be returns.
9595

0 commit comments

Comments
 (0)