Skip to content
Closed
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
18 changes: 17 additions & 1 deletion src/algorithms/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ def find_shortest_path(self, start: str, end: str) -> list[str]:

def find_last_node(nodes, edges):
"""This function receives a flow and returns the last node."""
return next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None)
try:
sources = {e["source"] for e in edges}
except TypeError:
# If any source is unhashable, fall back to original behavior to preserve exceptions/semantics
return next(
(n for n in nodes if all(e["source"] != n["id"] for e in edges)), None
)

# Use a generator with try-except to match original lazy evaluation behavior
def is_last_node(n):
try:
return n["id"] not in sources
except (KeyError, TypeError):
# Fall back to original check for this node to preserve exception behavior
return all(e["source"] != n["id"] for e in edges)

return next((n for n in nodes if is_last_node(n)), None)


def find_leaf_nodes(nodes: list[dict], edges: list[dict]) -> list[dict]:
Expand Down