mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 01:00:13 -04:00
1.8 KiB
1.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 65578fcf00322dbad5dee05b | Крок 43 | 20 | step-43 |
--description--
Створіть інструкцію if, щоб перевірити, чи відстань сусіднього вузла (другий елемент в обробленому кортежі) плюс відстань current менша за наразі відому відстань сусіднього вузла (перший елемент в обробленому кортежі).
Використайте ключове слово pass, щоб тимчасово заповнити тіло if.
--hints--
Ви повинні мати інструкцію if, щоб перевірити, чи distance + distances[current] менше за 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')