mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-25 02:14:11 -05:00
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 65578fcf00322dbad5dee05b | Step 43 | 20 | step-43 |
--description--
Create an if statement to check if the distance of the neighbor node (the second item in the processed tuple) plus the distance of current is less than the currently known distance of the neighbor node (the first item in the processed tuple).
Use the pass keyword to temporarily fill the body of the if.
--hints--
You should have an if statement to check if distance + distances[current] is less than distances[node].
({ test: () => {
const shortest = __helpers.python.getDef(code, "shortest_path");
const {function_body} = shortest;
assert(function_body.match(/^(\s*)for\s+\w+\s*,\s*\w+\s+in\s+graph\s*\[\s*current\s*\]\s*:\s*^\1(\s{4})if\s+distance\s*\+\s*distances\s*\[\s*current\s*\]\s*<\s*distances\s*\[\s*node\s*\]\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)
while unvisited:
current = min(unvisited, key=distances.get)
--fcc-editable-region--
for node, distance in graph[current]:
pass
--fcc-editable-region--
print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
#shortest_path(my_graph, 'A')