Skip to content
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

[Upgrade Ruff] Fix all errors raised from ruff #8879

Merged
merged 24 commits into from
Jul 22, 2023
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ repos:
- id: auto-walrus

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.278
rev: v0.0.280
hooks:
- id: ruff

2 changes: 1 addition & 1 deletion cellular_automata/game_of_life.py
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
if pt:
if alive < 2:
state = False
elif alive == 2 or alive == 3:
elif alive in {2, 3}:
state = True
elif alive > 3:
state = False
2 changes: 1 addition & 1 deletion data_structures/binary_tree/red_black_tree.py
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ def _insert_repair(self) -> None:
self.grandparent.color = 1
self.grandparent._insert_repair()

def remove(self, label: int) -> RedBlackTree:
def remove(self, label: int) -> RedBlackTree: # noqa: PLR0912
"""Remove label from this tree."""
if self.label == label:
if self.left and self.right:
4 changes: 2 additions & 2 deletions data_structures/trie/radix_tree.py
Original file line number Diff line number Diff line change
@@ -156,7 +156,7 @@ def delete(self, word: str) -> bool:
del self.nodes[word[0]]
# We merge the current node with its only child
if len(self.nodes) == 1 and not self.is_leaf:
merging_node = list(self.nodes.values())[0]
merging_node = next(iter(self.nodes.values()))
self.is_leaf = merging_node.is_leaf
self.prefix += merging_node.prefix
self.nodes = merging_node.nodes
@@ -165,7 +165,7 @@ def delete(self, word: str) -> bool:
incoming_node.is_leaf = False
# If there is 1 edge, we merge it with its child
else:
merging_node = list(incoming_node.nodes.values())[0]
merging_node = next(iter(incoming_node.nodes.values()))
incoming_node.is_leaf = merging_node.is_leaf
incoming_node.prefix += merging_node.prefix
incoming_node.nodes = merging_node.nodes
2 changes: 1 addition & 1 deletion divide_and_conquer/convex_hull.py
Original file line number Diff line number Diff line change
@@ -266,7 +266,7 @@ def convex_hull_bf(points: list[Point]) -> list[Point]:
points_left_of_ij = points_right_of_ij = False
ij_part_of_convex_hull = True
for k in range(n):
if k != i and k != j:
if k not in {i, j}:
det_k = _det(points[i], points[j], points[k])

if det_k > 0:
18 changes: 9 additions & 9 deletions graphs/directed_and_undirected_(weighted)_graph.py
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ def dfs(self, s=-2, d=-1):
stack = []
visited = []
if s == -2:
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
ss = s
@@ -87,7 +87,7 @@ def bfs(self, s=-2):
d = deque()
visited = []
if s == -2:
s = list(self.graph)[0]
s = next(iter(self.graph))
d.append(s)
visited.append(s)
while d:
@@ -114,7 +114,7 @@ def topological_sort(self, s=-2):
stack = []
visited = []
if s == -2:
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
ss = s
@@ -146,7 +146,7 @@ def topological_sort(self, s=-2):
def cycle_nodes(self):
stack = []
visited = []
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
parent = -2
@@ -199,7 +199,7 @@ def cycle_nodes(self):
def has_cycle(self):
stack = []
visited = []
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
parent = -2
@@ -305,7 +305,7 @@ def dfs(self, s=-2, d=-1):
stack = []
visited = []
if s == -2:
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
ss = s
@@ -353,7 +353,7 @@ def bfs(self, s=-2):
d = deque()
visited = []
if s == -2:
s = list(self.graph)[0]
s = next(iter(self.graph))
d.append(s)
visited.append(s)
while d:
@@ -371,7 +371,7 @@ def degree(self, u):
def cycle_nodes(self):
stack = []
visited = []
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
parent = -2
@@ -424,7 +424,7 @@ def cycle_nodes(self):
def has_cycle(self):
stack = []
visited = []
s = list(self.graph)[0]
s = next(iter(self.graph))
stack.append(s)
visited.append(s)
parent = -2
2 changes: 1 addition & 1 deletion graphs/edmonds_karp_multiple_source_and_sink.py
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ def _algorithm(self):
vertices_list = [
i
for i in range(self.verticies_count)
if i != self.source_index and i != self.sink_index
if i not in {self.source_index, self.sink_index}
]

# move through list
2 changes: 1 addition & 1 deletion maths/factorial.py
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ def factorial_recursive(n: int) -> int:
raise ValueError("factorial() only accepts integral values")
if n < 0:
raise ValueError("factorial() not defined for negative values")
return 1 if n == 0 or n == 1 else n * factorial(n - 1)
return 1 if n in {0, 1} else n * factorial(n - 1)


if __name__ == "__main__":
2 changes: 1 addition & 1 deletion maths/primelib.py
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ def prime_factorization(number):

quotient = number

if number == 0 or number == 1:
if number in {0, 1}:
ans.append(number)

# if 'number' not prime then builds the prime factorization of 'number'
2 changes: 1 addition & 1 deletion other/davisb_putnamb_logemannb_loveland.py
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ def find_unit_clauses(
unit_symbols = []
for clause in clauses:
if len(clause) == 1:
unit_symbols.append(list(clause.literals.keys())[0])
unit_symbols.append(next(iter(clause.literals.keys())))
else:
f_count, n_count = 0, 0
for literal, value in clause.literals.items():
16 changes: 10 additions & 6 deletions project_euler/problem_009/sol3.py
Original file line number Diff line number Diff line change
@@ -28,12 +28,16 @@ def solution() -> int:
31875000
"""

return [
a * b * (1000 - a - b)
for a in range(1, 999)
for b in range(a, 999)
if (a * a + b * b == (1000 - a - b) ** 2)
][0]
return next(
iter(
[
a * b * (1000 - a - b)
for a in range(1, 999)
for b in range(a, 999)
if (a * a + b * b == (1000 - a - b) ** 2)
]
)
)


if __name__ == "__main__":
2 changes: 1 addition & 1 deletion quantum/ripple_adder_classic.py
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ def ripple_adder(
res = qiskit.execute(circuit, backend, shots=1).result()

# The result is in binary. Convert it back to int
return int(list(res.get_counts())[0], 2)
return int(next(iter(res.get_counts())), 2)


if __name__ == "__main__":
2 changes: 1 addition & 1 deletion strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
if i == 0 and j == 0:
return []
else:
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
if ops[i][j][0] in {"C", "R"}:
seq = assemble_transformation(ops, i - 1, j - 1)
seq.append(ops[i][j])
return seq
4 changes: 1 addition & 3 deletions web_programming/convert_number_to_words.py
Original file line number Diff line number Diff line change
@@ -90,9 +90,7 @@ def convert(number: int) -> str:
else:
addition = ""
if counter in placevalue:
if current == 0 and ((temp_num % 100) // 10) == 0:
addition = ""
else:
if current != 0 and ((temp_num % 100) // 10) != 0:
addition = placevalue[counter]
if ((temp_num % 100) // 10) == 1:
words = teens[current] + addition + words