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

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
65577791ad8c26a7705e2919 Step 34 20 step-34

--description--

With a dictionary comprehension, you can create a dictionary starting from an existing dictionary:

{key: val for key in dict}

In the example above, val is the value that key will have in the new dictionary, and dict is the existing dictionary.

You want to keep track of the paths between the starting node and each other node.

After the distances variable, create a paths variable and assign it a dictionary with all the keys from graph. Assign an empty list to each key and use a dictionary comprehension to build your dictionary.

--hints--

You should have a variable named paths.

({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^\s{4}paths\s*\=/m));
  }
})

Your paths variable should use the dictionary comprehension syntax to assign an empty list to each node in graph.

({ test: () =>  {
    const {function_body} = __helpers.python.getDef(code, "shortest_path");       
    assert(function_body.match(/^\s{4}paths\s*\=\s*\{\s*(\w+)\s*:\s*\[\s*\]\s+for\s+\1\s+in\s+graph\s*\}/m));
  }
})

--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)]
}

--fcc-editable-region--
def shortest_path(graph, start):
    unvisited = list(graph)
    distances = {}

    print(f'Unvisited: {unvisited}\nDistances: {distances}')

shortest_path(my_graph, 'A')
--fcc-editable-region--