There is an undirected graph with a list of tuples where each element is a tuple in the form of (id1,id2). I have to find whether a new (s,t) is present in the list of tuples which contains all the edges and add the tuple if it is not present. edges is the list of tuples
def add_edge(s: str, t: str) -> None:
if (s,t) or (t,s) not in edges:
edges.append((s,t))
return edges
But this fails in acoounting for duplicates of the form (a,b) = (b,a)
CodePudding user response:
this will work well
def add_edge(s: str, t: str) -> None:
if (s,t) not in edge and (t,s) not in edges:
edges.append((s,t))
return edges
CodePudding user response:
if (s,t) or (t,s) not in edges
This is not doing what you think. (s,t) is treated as a separate condition all on its own. So the statement is really this:
(s,t)
or
(t,s) not in edges
And since (s,t) is a non-empty sequence, the entire statement is automatically true.
