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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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?
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:
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?
In step 3, we remove the vertices we’ve already visited by checking with the seen dictionary. Here’s the code they consider correct: next_vertices = [vertex for vertex in next_vertices if vertex not in seen]
How come we’re able to compare the elements in a list with elements in a dictionary? next_vertices is a list, and seen is a dictionary. Do they automatically assume we’re comparing with the keys?
so the argument start_vertex and end_vertex of find_path are strings.
and that’s how seen is being amended as a dictionary (with current_vertex being equal a string too.
seen[current_vertex] = True
So current_vertex is a STRING! Not a Vertex object.
How come the below works then ?
next_vertices = [vertex for vertex in next_vertices if vertex not in seen]
Shouldn’t it be
next_vertices = [vertex for vertex in next_vertices if vertex.value not in seen]
Callan prints twice because it is put into the list the first time as an edge of peel and then again as an edge of harwick. After the first while loop:
seen = {peel: True}
start = [harwick, callan]
then the second while loop executes start.pop(0), harwick’s edges (peel and callan) are put into start. Peel doesn’t make it in because of the conditional but callan makes it in again. Hence:
seen = {peel: True, harwick: True}
start = [callan, callan]
while loop three pops callan, and then loop four pops another callan.
Also checkpoint two is still written incorrectly… it’s September 2023 now…