Looping with function having an object as a parameter

asked 2 years ago

vidyarthi gravatar image

Consider the following code:

def Sier(H):
      for i in list(range(1,H.order())):
           H.disjoint_union(H)
      return H
f=Sier(graphs.CompleteGraph(5))
f.order()

I get the output as 5. But, what I actually wanted was:

def Sier(n,k):
    l=list(range(1,k))
    g=graphs.CompleteGraph(n)
    for i in l:
        g=g.disjoint_union(graphs.CompleteGraph(n))
    return g
 k=Sier(5,5)
 k.order()

which gives me the correct output as 25. I ask as to what is the problem with the first code, like how to get k disjoint copies of a graph H with order k?

Preview: (hide)

Comments

You do not make any assignment in the first code and your graph H remains unchanged.

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )