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

Tell us what’s happening:

Creating another forum post as pkdvalis asked.

Yes, I have read your solution on that other forum post. However, I can’t make any sense of it. This has been the most confusing language for me to get a grasp on so far. I’ve gotten this far in the curriculum, but I feel like I haven’t learned anything. I spend most of my time on forums searching for what others make sense of the questions. I seem to always think the questions are asking for X when everyone else can clearly see they want Y instead

Even when I find my way to the answer, I’m left scratching my head at how anyone came to that conclusion based off the questions. Very puzzling.

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]:
            if distance + distances[current] < distances[node]:
                distances[node] = distance + distances[current]

# User Editable Region

                if paths[node][-1] == node:
                    paths[node] = paths[current]
                if paths[node] > node and paths[node][-1] != []:
                    paths[node] = paths[current]

# User Editable Region

                else:
                    paths[node].extend(paths[current])
                paths[node].append(node)
        unvisited.remove(current)
    
    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/122.0.0.0 Safari/537.36

Challenge Information:

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

2 Likes
if paths[node][-1] == node:
                    paths[node] = paths[current]

Add an additional condition to your nested if statement to ensure that paths[node] is non-empty before accessing paths[node][-1].

You’ve added:

paths[node] > node

Should it be greater than node or greater than 0?
Or should it equal true?

How can you tell if a list is empty or not?

Check some examples here about truthiness:
https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/

The main point being that if a list is empty is “Falsey” and if a list is not empty it’s equivalent to a “True” value. Frankly there are a few ways to do this, but here are two examples that are equivalent:

list = [1,2,3]

if list != []:
    print("The list is not empty")

if list:
    print("The list is not empty")

You can test this out

I’ve gone through that link you shared. It seems that what this lesson is trying to teach is that you can take a simple code that is easily readable by others, make it complicated and unreadable to accomplish the same result. What is the point of that?

Still at a loss for this one.

if paths[node][-1] == node:
                    paths[node] = paths[current]

I’ve tried changing the above ‘==’ to a ‘<’ and a ‘>’. none pass.

Well you were correct the first time, it’s a compound statement using “and”.

You just need to change this part:

paths[node] > node

but check if paths[node] is non-empty (or true) instead of greater than node.

hmm…

if paths[node] < [] and paths[node][-1] != []:
                    paths[node] = paths[current]

I’ve changed out the ‘<’ for ‘!=’, ‘==’, ‘>’. Nothing has passed.

I’m not an expert and I can’t help you on this one, but I thought I could tell you that discouragement and frustration is normal for all of us and you’re not the only one having a hard time once in a while. It can seem like that because only the people who have answers write comments, but I can assure you that coding has all of us scratching our heads more often than we want to admit. Just breathe, think calmly and never give up. There’s always a solution, and it won’t take long to find it.
Anyway, thought I’d let you know you’re not alone.

2 Likes

My mistake, we’ve changed too much here. Here is the original line again:

                if paths[node][-1] == node:

Don’t change this part: “paths[node][-1] == node”

You need to add a condition using “and”

Add an additional condition to ensure that paths[node] is non-empty before accessing paths[node][-1].

Again, here are two examples of how to check if a list is not empty:

if list != []:
    print("The list is not empty")

if list:
    print("The list is not empty")

I am still not understanding…

I now have

                if paths[node][-1] == node:
                    paths[node] = paths[current]
                if paths[node] != [] and paths[node][-1] != []:
                    paths[node] = paths[current]

And it is telling me: You should add paths[node] as the first condition to your nested if statement. Use the and operator to combine your conditions.

But from my understanding… thats what I’ve done already?

Between this and Tower of Hanoi step 39, I am at a complete stand still…

1 Like

Can’t believe you guys are still working on this! The persistence is amazing. I think you’re getting close…

The hint is telling you exactly what to do:

You should add paths[node] as the first condition to your nested if statement.

Instead of adding to the existing if statement, you’ve added a whole new if statement. And where did this come from “paths[node][-1] != []” ?

Here is the original line again, reset the step to get back to it:

if paths[node][-1] == node:

Now, edit that if statement, add paths[node] as the first condition, using and to connect the two conditions.

if <condition> and <condition>:
1 Like

I now have:

if paths[node][-1] == node and paths[node] = paths[current]:

Still not passing…

Whats crazy is the amount of forum posts I’ve seen stuck on this same lesson, going through those posts are more confusing than trying to figure out this post.

Two things here

Add an additional condition to your nested if statement to ensure that paths[node] is non-empty before accessing paths[node][-1].

  1. “ensure that paths[node] is non-empty BEFORE accessing paths[node][-1].”

Your order is wrong. Check paths[node] first.

  1. paths[node] = paths[current] Does this check if it’s non-empty?

Look carefully at this example.

if list:
    print("The list is not empty")

To explain this a bit more. A list will evaluate to true if it’s non-empty. So, the only thing you need to put as a condition for an if statement, is the list.

list =1, 2, 3

if list:     #this will be True because the list is not empty.

newlist = []

if newlist:
    print("This will not print, because the list is ")
    print("empty and therefore False")

https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/

I went through the truthy falsy link you shared a few days ago. It did not help with this lesson.

I do not understand any of the above honestly… which raises the question of how I got this far to begin with… maybe python is not for me. I can’t make any sense of it.

Do you understand this example, of how the if statement checks if the list is not empty?

Using the example above, how would you write an if statement to check if paths[node] is not empty?

Try to make a specific question of what it is you don’t understand, maybe I’m explaining the wrong things.

Since the website update, I’ve decided that I will go back to the 2nd part of the curriculum and do the added Beta for javascript, and work my way down from there to get back to Python. It is just too confusing. I have front end and data visualization done already, so after Java I have relational database, back end development, and quality assurance before I will revisit this python section.

Thank you anyway.