ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 13 May 2022 23:45:31 +0200Looping with function having an object as a parameterhttps://ask.sagemath.org/question/62434/looping-with-function-having-an-object-as-a-parameter/ 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$?Fri, 13 May 2022 20:16:10 +0200https://ask.sagemath.org/question/62434/looping-with-function-having-an-object-as-a-parameter/Comment by Max Alekseyev for <p>Consider the following code:</p>
<pre><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()
</code></pre>
<p>I get the output as 5.
But, what I actually wanted was:</p>
<pre><code>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()
</code></pre>
<p>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$?</p>
https://ask.sagemath.org/question/62434/looping-with-function-having-an-object-as-a-parameter/?comment=62440#post-id-62440You do not make any assignment in the first code and your graph `H` remains unchanged.Fri, 13 May 2022 23:45:31 +0200https://ask.sagemath.org/question/62434/looping-with-function-having-an-object-as-a-parameter/?comment=62440#post-id-62440