Back to Freecodecamp

Step 37

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

latest1.1 KB
Original Source

--description--

Add \nPaths: {paths} at the end of the f-string passed to the print call, so that it prints the paths variable, too.

--hints--

You should modify your existing print call by adding \nPaths: {paths} at the end of the f-string.

js
({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^\s{4}print\s*\(\s*f("|')Unvisited:\s*\{\s*unvisited\s*\}\\nDistances:\s\{\s*distances\s*\}\\nPaths:\s\{\s*paths\s*\}\1\s*\)/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 = list(graph)
    distances = {node: 0 if node == start else float('inf') for node in graph}
    paths = {node: [] for node in graph}
    paths[start].append(start)
    
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
    
shortest_path(my_graph, 'A')
--fcc-editable-region--