Files
2024-05-22 17:27:37 +02:00

1.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
65578f895f2a65ba7a916804 Step 42 20 step-42

--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.

({ 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--

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')