Skip to content

Commit 93fb555

Browse files
Enable ruff SIM102 rule (TheAlgorithms#11341)
* Enable ruff SIM102 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f8a9489 commit 93fb555

File tree

13 files changed

+73
-50
lines changed

13 files changed

+73
-50
lines changed

data_structures/arrays/sudoku_solver.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ def eliminate(values, s, d):
9292
dplaces = [s for s in u if d in values[s]]
9393
if len(dplaces) == 0:
9494
return False ## Contradiction: no place for this value
95-
elif len(dplaces) == 1:
96-
# d can only be in one place in unit; assign it there
97-
if not assign(values, dplaces[0], d):
98-
return False
95+
# d can only be in one place in unit; assign it there
96+
elif len(dplaces) == 1 and not assign(values, dplaces[0], d):
97+
return False
9998
return values
10099

101100

data_structures/stacks/balanced_parentheses.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ def balanced_parentheses(parentheses: str) -> bool:
1919
for bracket in parentheses:
2020
if bracket in bracket_pairs:
2121
stack.push(bracket)
22-
elif bracket in (")", "]", "}"):
23-
if stack.is_empty() or bracket_pairs[stack.pop()] != bracket:
24-
return False
22+
elif bracket in (")", "]", "}") and (
23+
stack.is_empty() or bracket_pairs[stack.pop()] != bracket
24+
):
25+
return False
2526
return stack.is_empty()
2627

2728

graphs/a_star.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ def search(
7575
for i in range(len(DIRECTIONS)): # to try out different valid actions
7676
x2 = x + DIRECTIONS[i][0]
7777
y2 = y + DIRECTIONS[i][1]
78-
if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]):
79-
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
80-
g2 = g + cost
81-
f2 = g2 + heuristic[x2][y2]
82-
cell.append([f2, g2, x2, y2])
83-
closed[x2][y2] = 1
84-
action[x2][y2] = i
78+
if (
79+
x2 >= 0
80+
and x2 < len(grid)
81+
and y2 >= 0
82+
and y2 < len(grid[0])
83+
and closed[x2][y2] == 0
84+
and grid[x2][y2] == 0
85+
):
86+
g2 = g + cost
87+
f2 = g2 + heuristic[x2][y2]
88+
cell.append([f2, g2, x2, y2])
89+
closed[x2][y2] = 1
90+
action[x2][y2] = i
8591
invpath = []
8692
x = goal[0]
8793
y = goal[1]

graphs/bi_directional_dijkstra.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ def pass_and_relaxation(
3636
queue.put((new_cost_f, nxt))
3737
cst_fwd[nxt] = new_cost_f
3838
parent[nxt] = v
39-
if nxt in visited_backward:
40-
if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
41-
shortest_distance = cst_fwd[v] + d + cst_bwd[nxt]
39+
if (
40+
nxt in visited_backward
41+
and cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance
42+
):
43+
shortest_distance = cst_fwd[v] + d + cst_bwd[nxt]
4244
return shortest_distance
4345

4446

other/davis_putnam_logemann_loveland.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ def assign(self, model: dict[str, bool | None]) -> None:
6464
value = model[symbol]
6565
else:
6666
continue
67-
if value is not None:
68-
# Complement assignment if literal is in complemented form
69-
if literal.endswith("'"):
70-
value = not value
67+
# Complement assignment if literal is in complemented form
68+
if value is not None and literal.endswith("'"):
69+
value = not value
7170
self.literals[literal] = value
7271

7372
def evaluate(self, model: dict[str, bool | None]) -> bool | None:

project_euler/problem_033/sol1.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def fraction_list(digit_len: int) -> list[str]:
4444
last_digit = int("1" + "0" * digit_len)
4545
for num in range(den, last_digit):
4646
while den <= 99:
47-
if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
48-
if is_digit_cancelling(num, den):
49-
solutions.append(f"{num}/{den}")
47+
if (
48+
(num != den)
49+
and (num % 10 == den // 10)
50+
and (den % 10 != 0)
51+
and is_digit_cancelling(num, den)
52+
):
53+
solutions.append(f"{num}/{den}")
5054
den += 1
5155
num += 1
5256
den = 10

project_euler/problem_037/sol1.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ def validate(n: int) -> bool:
8585
>>> validate(3797)
8686
True
8787
"""
88-
if len(str(n)) > 3:
89-
if not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])):
90-
return False
88+
if len(str(n)) > 3 and (
89+
not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3]))
90+
):
91+
return False
9192
return True
9293

9394

project_euler/problem_107/sol1.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ def prims_algorithm(self) -> Graph:
8181
while len(subgraph.vertices) < len(self.vertices):
8282
min_weight = max(self.edges.values()) + 1
8383
for edge, weight in self.edges.items():
84-
if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices):
85-
if weight < min_weight:
86-
min_edge = edge
87-
min_weight = weight
84+
if (edge[0] in subgraph.vertices) ^ (
85+
edge[1] in subgraph.vertices
86+
) and weight < min_weight:
87+
min_edge = edge
88+
min_weight = weight
8889

8990
subgraph.add_edge(min_edge, min_weight)
9091

project_euler/problem_207/sol1.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ def solution(max_proportion: float = 1 / 12345) -> int:
8888
total_partitions += 1
8989
if check_partition_perfect(partition_candidate):
9090
perfect_partitions += 1
91-
if perfect_partitions > 0:
92-
if perfect_partitions / total_partitions < max_proportion:
93-
return int(partition_candidate)
91+
if (
92+
perfect_partitions > 0
93+
and perfect_partitions / total_partitions < max_proportion
94+
):
95+
return int(partition_candidate)
9496
integer += 1
9597

9698

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
1818
"S105", # Possible hardcoded password: 'password'
1919
"S113", # Probable use of requests call without timeout -- FIX ME
2020
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes -- FIX ME
21-
"SIM102", # Use a single `if` statement instead of nested `if` statements -- FIX ME
2221
"SLF001", # Private member accessed: `_Iterator` -- FIX ME
2322
"UP038", # Use `X | Y` in `{}` call instead of `(X, Y)` -- DO NOT FIX
2423
]

0 commit comments

Comments
 (0)