Back to Freecodecamp

Step 27

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

latest1.1 KB
Original Source

--description--

While the algorithm explores the graph, it should keep track of the currently known shortest distance between the starting node and the other nodes.

Before your for loop, create a new variable named distances and assign it an empty dictionary.

--hints--

You should have a variable named distances.

js
({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^(\s*)distances\s*=.*(?=^\1for.*:)/ms));
  }
})

Your distances variable should be an empty dictionary.

js
({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^(\s*)distances\s*=\s*\{\s*\}.*(?=^\1for.*:)/ms));
  }
})

--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 = []
    for node in graph:
        unvisited.append(node)
--fcc-editable-region--