FAQ: Graphs: Python - Refactoring Path-Finding

This community-built FAQ covers the “Refactoring Path-Finding” exercise from the lesson “Graphs: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Refactoring Path-Finding

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Assignment 2 says:

Replace the commented code after we declare current_vertex . Set the key of the current_vertex as True in our seen dictionary.

Should that be: “Set the value of the current_vertex as True in our seen dictionary.”? Or am I missing something here?

1 Like

You have it correct. The dictionary is seen, a key is current_vertex, and the associated value is True.

4 Likes

It is February 29, 2020 and the text still erroneously reads as follows.
Replace the commented code after we declare current_vertex . Set the key of the current_vertex as True in our seen dictionary.

Was there any reason for using a dictionary and not a list for the ‘seen’ variable? I tried it with the list and it gave me the same result, and I can’t think of any reasons it wouldn’t in other circumstances. It seems a lot easier to just use a list, am I missing something?

Why is

Visiting callan

printed twice when the script.py is run?

This is the command

railway.add_vertex(callan)
railway.add_vertex(peel)
railway.add_vertex(harwick)
railway.add_vertex(ulfstead)

railway.add_edge(peel, harwick)
railway.add_edge(harwick, callan)
railway.add_edge(callan, peel)

peel_to_ulfstead_path_exists = railway.find_path('peel', 'ulfstead')

that runs this piece of code in graph.py

  def find_path(self, start_vertex, end_vertex):
    start = [start_vertex]
    seen = {}
    while len(start) > 0:
      current_vertex = start.pop(0)
      seen[current_vertex] = True
      print("Visiting " + current_vertex)
      if current_vertex == end_vertex:
        return True
      else:
        vertex = self.graph_dict[current_vertex]
        next_vertices = vertex.get_edges()
        next_vertices = [vertex for vertex in next_vertices if vertex not in seen]
        start.extend(next_vertices)
    return False

I would have assumed that after pop(0) removed “callan” from the “start” list that the while loop would end and return False. There would be no more next_vertices with which to extend the start list.

However, it runs one more time for the following results:

Visiting peel
Visiting harwick
Visiting callan
Visiting callan

Thanks!

These courses are poorly made sometimes. They just ask you to follow the step like a robot. If you do the same, another way, you get an error even if it is correct…

Instead of doing:

next_veritcles = vertex.get_edges()
        next_vertices = [vertex for vertex in next_vertices if vertex not in seen]

I did:

next_vertices = [edge for edgein vertex.get_edges() if edge not in seen]

So I tried:

next_veritcles = vertex.get_edges()
next_vertices = [edge for edge in next_vertices if edge not in seen]

So just because I don’t have the same name they want me to have for a temporary local variable, I get an error?

1 Like