Skip to content

Commit c63a974

Browse files
committed
refactor: get the successors using a list comprehension
1 parent 3ccf285 commit c63a974

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

Diff for: graphs/greedy_best_first.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,22 @@ def get_successors(self, parent: Node) -> list[Node]:
148148
"""
149149
Returns a list of successors (both in the grid and free spaces)
150150
"""
151-
successors = []
152-
for action in delta:
153-
pos_x = parent.pos_x + action[1]
154-
pos_y = parent.pos_y + action[0]
155-
151+
return [
152+
Node(
153+
pos_x,
154+
pos_y,
155+
self.target.pos_x,
156+
self.target.pos_y,
157+
parent.g_cost + 1,
158+
parent,
159+
)
160+
for action in delta
156161
if (
157-
0 <= pos_x <= len(self.grid[0]) - 1
158-
and 0 <= pos_y <= len(self.grid) - 1
162+
0 <= (pos_x := parent.pos_x + action[1]) < len(self.grid[0])
163+
and 0 <= (pos_y := parent.pos_y + action[0]) < len(self.grid)
159164
and self.grid[pos_y][pos_x] == 0
160-
):
161-
successors.append(
162-
Node(
163-
pos_x,
164-
pos_y,
165-
self.target.pos_x,
166-
self.target.pos_y,
167-
parent.g_cost + 1,
168-
parent,
169-
)
170-
)
171-
return successors
165+
)
166+
]
172167

173168
def retrace_path(self, node: Node | None) -> Path:
174169
"""

0 commit comments

Comments
 (0)