Command Palette

Search for a command to run...

Hectal
Phase 12: Advanced Patterns
RAG and Vector Search · Exercise 3 of 5 PreviousNext
Intermediate

Walk a graph with a hop limit

triples is a list of (left, relation, right). Write reachable(start, triples, max_hops) that follows edges from left to right for up to max_hops hops and returns the sorted list of nodes reached (not including start).

A node must be visited once only, so a loop in the data cannot make the walk run for ever.

What Check tests

  1. One hop finds direct neighbours
  2. More hops go further
  3. Zero hops reaches nothing
  4. A loop does not run for ever
  5. A missing start gives an empty list

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.