Files
2024-05-29 10:06:17 -07:00

1.8 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
657891ab9c1903f4e55433ba Крок 44 20 step-44

--description--

Якщо умова нової інструкції if правдива, то найкоротший шлях до сусіднього вузла знайдено.

Видаліть pass в межах нового блоку if та перепризначте відстань сусіднього вузла до суми відстані сусіднього вузла плюс відстань current.

--hints--

Призначте distance + distances[current] до distances[node] в межах нової інструкції if.

({ test: () =>  {
    const commentless_code = __helpers.python.removeComments(code);
    const {block_body} = __helpers.python.getBlock(commentless_code, /if\s+distance\s*\+\s*distances\s*\[\s*current\s*\]\s*<\s*distances\s*\[\s*node\s*\]\s*/);
    assert(block_body.match(/^\s+distances\s*\[\s*node\s*\]\s*=\s*distance\s*\+\s*distances\s*\[\s*current\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)]
}

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)
        for node, distance in graph[current]:
--fcc-editable-region--
            if distance + distances[current] < distances[node]:
                pass
--fcc-editable-region--
    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')

#shortest_path(my_graph, 'A')