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

Tell us what’s happening:

I get this hint when my code doesn’t pass:
“You should use the ternary syntax to assign [target] when target is truthy, and graph otherwise to your targets_to_print variable.”

I’m unsure what I’m doing wrong :confused:

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, target = ''):
    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]
                if paths[node] and paths[node][-1] == node:
                    paths[node] = paths[current][:]
                else:
                    paths[node].extend(paths[current])
                paths[node].append(node)
        unvisited.remove(current)

# User Editable Region

    
    targets_to_print = [target if target else graph]
    
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/128.0.0.0 Safari/537.36 Edg/128.0.0.0

Challenge Information:

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

Check where you have the closing parenthesis.

I’m not sure what you mean?

Instructions:

use the ternary syntax to assign [target] when target is truthy, and graph otherwise to your targets_to_print variable

You wrote this:

So, are you sure that the above assigns [target], when target is truthy?

To be more precise, the problem is when target is falsy. Because then you won’t be assigning graph, but [graph]

Wouldn’t what I’ve written be the same as this?

if target:
    targets_to_print = target
else:
    targets_to_print = graph

I think I might have a misunderstanding with how the ternary syntax is used :confused:

I asked bing, it gave me this:
targets_to_print = [target] if target else [graph]

Which also doesn’t work, but @Dario_DC response lead me to mess with the [] a little.

I’ve got it to pass, but have no idea why it passes :confused:

Hoping something will click the more I practice

Thank you @Dario_DC :smiley:

I think you are focusing on the wrong part. If you use the square brackets, you are creating a list.

What you wrote is equivalent to:

if target:
    targets_to_print = [target]
else:
    targets_to_print = [graph]

Then you’ll see that you want to iterate on targets_to_print, so you need it to be an iterable. But graph is already an iterable (a dictionary). So [graph] would be a dictionary nested within a list, which is not what you want.

Therefore you want something equivalent to:

if target:
    targets_to_print = [target]
else:
    targets_to_print = graph

This part is a bit tricky because you don’t know exactly what will happen in the following steps. I hope this helps to clarify your doubts.

1 Like