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
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")
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.
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.
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.
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")
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.
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.