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

I have tried different combinations using unvisited == 0, != 0, != , != ’ ’ and my code does not pass. I even tried While unvisited: Still does not pass. Please what else could I do?

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)
    
    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    while unvisited != 0:
    pass
#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/120.0.0.0 Safari/537.36

Challenge Information:

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

unvisited is a list. You cannot compare a list with an integer number because it’s meaningless. You could compare len(unvisited) (which returns the number of items included in the list) with an integer but the most pythonic way of doing this step is to use simply the list itself as the condition. If the list is empty, the condition is false an the loop does not run anymore. Also indent pass.

By the way, now I realize that this aspect is not well explained in the course and it should be improved.
If you want to run some code while the list my_list contains elements, you can write:

while len(my_list) > 0:
    pass

But this is the same of writing:

while my_list:
    pass

because my_list evaluates to True when it contains element, therefore when len(my_list) > 0.

Hope this can clarify.

2 Likes

@Dario_DC , we are getting there. Lets keep on trying. I entered your code suggestion of

While len(unvisited) > 0: the code is yet to pass.

We need to hang in there. Thanks for your patience

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/120.0.0.0 Safari/537.36

Challenge Information:

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

I’m sorry @zack3 what I wanted to explain is that

is valid code, but here the test is too strict and requires you to do it in a different and “simpler” way. You don’t need to use len() or to compare the list with something else. If you use the list itself as the condition:

  • when the list contains something, the condition evaluates to True and the loop runs
  • when the list is empty, the condition evaluates to False and the loop does not run

–removed–
this is the solution for anyone who couldn’t figure it out for 2 hours like me(indent the pass).

Please don’t post solution code, thanks!