Back to Freecodecamp

Step 42

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

latest1.3 KB
Original Source

--description--

After the current variable assignment, create a for loop to iterate over the tuples in the graph[current] list. You will need two iterating variables for that. Remember to use pass to fill the loop body.

--hints--

You should create a for loop to iterate over the tuples items in the graph[current] list. Use two iterating variables and don't forget to add the pass keyword.

js
({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^(\s*)while\s+unvisited\s*:.*^\1\1for\s+\w+\s*,\s*\w+\s+in\s+graph\s*\[\s*current\s*\]\s*:\s*^\1\1\1pass/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)]
}

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)
--fcc-editable-region--    
    while unvisited:
        current = min(unvisited, key=distances.get)
--fcc-editable-region--    
    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
#shortest_path(my_graph, 'A')