mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-07 15:03:20 -04:00
1.5 KiB
1.5 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 65577791ad8c26a7705e2919 | Schritt 29 | 20 | step-29 |
--description--
With a dictionary comprehension, you can create a dictionary starting from an existing dictionary:
{key: val for key in dict}
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 paths variable.
({ 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 shortest = __helpers.python.getDef(code, "shortest_path");
const {function_body} = shortest;
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--