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

1.8 KiB
Raw Blame History

id, title, challengeType, dashedName
id title challengeType dashedName
655774d01daeeaa1978b99d5 Крок 29 20 step-29

--description--

Вважається, що спочатку всі інші вузли в графі розташовані на нескінченній відстані від вихідного вузла, оскільки відстань ще не визначена.

Створіть умову else та призначте нескінченне значення до вузла в словнику distances. Для цього використайте функцію float() з аргументом у вигляді рядка 'inf', щоб створити число з рухомою комою, яке представляє додатну нескінченність.

--hints--

Ви повинні мати умову else.

({ test: () =>  {
    const shortest = __helpers.python.getDef(code, "shortest_path");
    const {function_body} = shortest;    
    assert(function_body.match(/^(\s*)if.*:.*^\1else\s*:/ms));
  }
})

Призначте float('inf') до distances[node] в межах нової умови else.

({ 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--