Back to Freecodecamp

Step 28

curriculum/challenges/english/blocks/learn-algorithm-design-by-building-a-shortest-path-algorithm/655774955b097ea14897db12.md

latest1.5 KB
Original Source

--description--

The distance from the starting node is zero, because the algorithm begins its assessment right from there.

After appending node to unvisited in your loop, create an if statement that triggers if the node is equal to the starting node. Then assign 0 to that node inside the distances dictionary.

--hints--

You should create an if statement that executes when node is equal to start.

js
({ test: () =>  {
    const commentless_code = __helpers.python.removeComments(code);
    const {block_body} = __helpers.python.getBlock(commentless_code, /for\s+node\s+in\s+graph\s*/);        
    assert(block_body.match(/unvisited\.append\s*\(\s*node\s*\)\s*^\s+if\s+(node\s*==\s*start|start\s*==\s*node)\s*:/m));
  }
})

Inside your new if statement you should assign 0 to the node in the distances dictionary.

js
({ test: () =>  {
   const commentless_code = __helpers.python.removeComments(code);
    const {block_body} = __helpers.python.getBlock(commentless_code, /if\s+(node\s*==\s*start|start\s*==\s*node)\s*/m);  
    assert(block_body.match(/^\s+distances\s*\[\s*node\s*\]\s*=\s*0/m));
  }
})

--seed--

--seed-contents--

py
my_graph = {
    'A': [('B', 3), ('D', 1)],
    'B': [('A', 3), ('C', 4)],
    'C': [('B', 4), ('D', 7)],
    'D': [('A', 1), ('C', 7)]
}

--fcc-editable-region--
def shortest_path(graph, start):
    unvisited = []
    distances = {}
    for node in graph:
        unvisited.append(node)
--fcc-editable-region--