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

hi,
I have a problem, the request for Step 34 is this:

Before the print call, create a while loop that runs while unvisited is not empty. Use the pass keyword to fill the loop body.

while len(unvisited)!=0 :
pass

while unvisited != :
pass

I don’t understand why this two-way to tell that unvisited is empty isn’t correct, maybe I don’t understand the request

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)]
}


# User Editable Region

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 len(unvisited)!=0 :
         pass

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

# User Editable Region

Your browser information:

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

Challenge Information:

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

I find the answer on internet. the answer is

while unvisited:
     pass

because if unvisited is empty the condition is false otherwise is true

I just had the same problem.
You are right but freeCodeCamp should change their test so that THERE’S MORE THAN ONE RIGHT WAY and if you mean a sepcific algorithm for any logical test you should explicitly mention it, otherwise allow us to use other methods so that we don’t waste learning time!!
Of course I don’t mean to be negative and appreciate every second I can use this website, but still it’s important

2 Likes

@shlomov1 Generally the instructions are pretty specific and there’s not that many ways to interpret them. How many ways are there to solve the instructions for this step?

In this case, these two patterns are equivalent:

if list:

if len(list) != 0:

You can see one way is a bit better. If you spend some time and learn both of these ways, then you will know them, so it’s not really wasted.

That said… since being launched all 5 topics opened for this step have made the same or similar error
https://forum.freecodecamp.org/search?q=Algorith%20-%20Step%2034%20in%3Atitle%20%23python

This might be a good case for opening an issue to discuss a change to the instructions to make it more clear.

Aha, it looks like something is already open:
" 34: Test needs to be expanded to allow multiple correct answers (e.g. while len(unvisited) != 0, while unvisited != [], while len(unvisited) > 0)"
https://github.com/freeCodeCamp/freeCodeCamp/issues/52582

If you want to learn more about opening a github issue for cases like this you can read about it here (scroll down a bit…)
https://www.freecodecamp.org/news/how-to-report-a-bug-to-freecodecamp/

You can see example issues here:
https://github.com/freeCodeCamp/freeCodeCamp/issues

2 Likes

For what it’s worth imo

if list:

Is a better solution and more Pythonic. I think it would be better to change the instructions to guide to this solution specifically.

1 Like

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