Learn Algorithm Design by Building a Shortest Path Algorithm - Step 39

Tell us what’s happening:

Describe your issue in detail here.
Hi team. Please assist with this.

Your code so far

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]:

/* User Editable Region */

            if true:
            distance + distances[current] = distances[node]
              

/* User Editable Region */

    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
#shortest_path(my_graph, 'A')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

Challenge Information:

Learn Algorithm Design by Building a Shortest Path Algorithm - Step 39
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-algorithm-design-by-building-a-shortest-path-algorithm/step-39`Preformatted text`

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

We see you have posted some code but did you have a question?

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Hello pkdvalis.
I am stuck at this step:

Step 39

Inside your new if block, delete pass and reassign the neighbor node distance to the sum of the neighbor node distance plus the distance of current.

My code so far:

if true:
distance + distances[current] = distances[node]

I honestly don’t understand how you’ve made it to this step if you:

Cannot write an if statement
Cannot add two variables together

You need to go back and learn the fundamentals if you want to continue with this. You need to understand each lesson, not just keep re-arranging the words until it passes.

You can review if statements here:
https://www.w3schools.com/python/gloss_python_if_statement.asp

If you want to add something to an existing variable the format is like this:
x = x + y

The result of the right hand side of the equals sign is stored in the variable on the left hand side.

Inside your new if block, delete pass and reassign the neighbor node distance to the sum of the neighbor node distance plus the distance of current.

if true:

Can you explain the intent of this line to me? And how you decided to write it like this based on the instructions?

I simply got confused. The issue was about indentation .

This is the code I wrote initially:

REMOVED

It did not pass, many times. I closed every windows. I started over. Now it passes.

I was confused by the plat form. My previous code was an attempt to transform the previous if statement.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

No need to try to convince me. You’re doing this for yourself, to learn.

If you could not see what was wrong with the code you posted, at this point, then you might not be learning effectively.

Sometimes the platform might not behave as expected. You can try to disable any kind of extensions and enable permissions for this site, or try a different browser, you might get better results.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.