mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 10:00:39 -04:00
1.5 KiB
1.5 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 655774d01daeeaa1978b99d5 | Step 29 | 20 | step-29 |
--description--
At the beginning, all the other nodes in the graph are considered to be at infinite distance from the source node, because the distance has not been determined yet.
Create an else clause and assign an infinite value to the node in the distances dictionary. For that, use the float() function with the string 'inf' as argument to generate a floating point number representing the positive infinity.
--hints--
You should have an else clause.
({ test: () => {
const shortest = __helpers.python.getDef(code, "shortest_path");
const {function_body} = shortest;
assert(function_body.match(/^(\s*)if.*:.*^\1else\s*:/ms));
}
})
You should assign float('inf') to distances[node] inside your new else clause.
({ test: () => {
const commentless_code = __helpers.python.removeComments(code);
const {block_body} = __helpers.python.getBlock(commentless_code, "else");
assert(block_body.match(/^\s+distances\s*\[\s*node\s*\]\s*=\s*float\s*\(\s*("|')inf\1\s*\)\s*$/));
}
})
--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 = []
distances = {}
for node in graph:
unvisited.append(node)
if node == start:
distances[node] = 0
--fcc-editable-region--