I am trying to print the distinct nodes in a graph when given a list of edges with the following:
def find_nodes(graph):
# get the distinct nodes from the edges
nodes = []
l = len(graph)
for i in range(l):
edge = graph[i]
n1 = edge[0]
n2 = edge[1]
if n1 not in nodes:
nodes.append(n1)
if n2 not in nodes:
nodes.append(n2)
return nodes
graph = ((1,2),(2,3), (3,1))
print find_nodes(graph)
But I only get (1,2) how am i missing the 3?
When I look at the text you’ve inserted, it looks like you’re mixing tabs and spaces as left-hand whitespace:
This can be confirmed by looking at the repr of each line:
This can result in lines not being indented to the level you think they are. Here’s what I get as a result from copying and pasting your input into a console:
It looks to me like the
return nodesline will be executed too early. Write the code into a file and use thepython -ttoption to check for whitespace problems.