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

Diff for: data_structures/arrays/sudoku_solver.py

+3-4
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

Diff for: data_structures/stacks/balanced_parentheses.py

+4-3
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

Diff for: graphs/a_star.py

+13-7
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]

Diff for: graphs/bi_directional_dijkstra.py

+5-3
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

Diff for: other/davis_putnam_logemann_loveland.py

+3-4
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:

Diff for: project_euler/problem_033/sol1.py

+7-3
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

Diff for: project_euler/problem_037/sol1.py

+4-3
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

Diff for: project_euler/problem_107/sol1.py

+5-4
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

Diff for: project_euler/problem_207/sol1.py

+5-3
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

Diff for: pyproject.toml

-1
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
]

Diff for: scheduling/shortest_job_first.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ def calculate_waitingtime(
3737
# Process until all processes are completed
3838
while complete != no_of_processes:
3939
for j in range(no_of_processes):
40-
if arrival_time[j] <= increment_time and remaining_time[j] > 0:
41-
if remaining_time[j] < minm:
42-
minm = remaining_time[j]
43-
short = j
44-
check = True
40+
if (
41+
arrival_time[j] <= increment_time
42+
and remaining_time[j] > 0
43+
and remaining_time[j] < minm
44+
):
45+
minm = remaining_time[j]
46+
short = j
47+
check = True
4548

4649
if not check:
4750
increment_time += 1

Diff for: scripts/validate_solutions.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ def added_solution_file_path() -> list[pathlib.Path]:
7171

7272

7373
def collect_solution_file_paths() -> list[pathlib.Path]:
74-
if os.environ.get("CI") and os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
75-
# Return only if there are any, otherwise default to all solutions
76-
if filepaths := added_solution_file_path():
77-
return filepaths
74+
# Return only if there are any, otherwise default to all solutions
75+
if (
76+
os.environ.get("CI")
77+
and os.environ.get("GITHUB_EVENT_NAME") == "pull_request"
78+
and (filepaths := added_solution_file_path())
79+
):
80+
return filepaths
7881
return all_solution_file_paths()
7982

8083

Diff for: web_programming/emails_from_url.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None
3030
if tag == "a":
3131
# Check the list of defined attributes.
3232
for name, value in attrs:
33-
# If href is defined, and not empty nor # print it.
34-
if name == "href" and value != "#" and value != "":
35-
# If not already in urls.
36-
if value not in self.urls:
37-
url = parse.urljoin(self.domain, value)
38-
self.urls.append(url)
33+
# If href is defined, not empty nor # print it and not already in urls.
34+
if (
35+
name == "href"
36+
and value != "#"
37+
and value != ""
38+
and value not in self.urls
39+
):
40+
url = parse.urljoin(self.domain, value)
41+
self.urls.append(url)
3942

4043

4144
# Get main domain name (example.com)

0 commit comments

Comments
 (0)