Files

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
655791847db8a9bd0b685f40 Step 43 20 step-43

--description--

Finally, outside the nested conditionals, append the neighbor node to its path.

--hints--

You should append node to paths[node] just after your else statement.

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