-2

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)

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
rashf
  • 27
  • 5

2 Answers2

0

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
Alex
  • 281
  • 2
  • 7
0
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.

John Gordon
  • 29,573
  • 7
  • 33
  • 58