18 Answers
New Answer18 Answers
New Answerno: primitives are passed by values, while objects are passed by references ^^ that's why only the first item is set to zero: the list id at the initial recursion call remain the same, but each other call pass the sliced list (wich is a copy) ;)
in 'Recurse' function you're trying to write in argument and you return nothing: a remain holding the same list/memory adress. only first item of list is set to zero, because the fact is that at each recursion steps, you're passing a copy of the argument list subset (slicing): you could print(id(a)) before Recursion call inside Recursion ;)
visph so I guess, the following is the way to achieve my desired outcome then. https://code.sololearn.com/cD8D3a49UxD6/?ref=app
to recursively set to zero all items of a list, rather use something like: def Recurse(a,i=0): if i != len(a): a[i] = 0 Recurse(a,i+1) however, a basic loop would be more efficient and not limited ^^
yes, your solution is valid too, but you doesn't need to return the Recursion call results ;)
with this one, you print another list inside 'recurs' function, but global list 'a' after recursion is [2,3,4] ;)
Lol, i am not after a completely new list but the original one modified. But thanks for trying.
its a good code, trying to solve it i learned about the nonlocal keyword & scoping, but i coulnt make them work for your problem. still its a nice code Sonic
def recurs(a): a.pop() a = [0] + a if a == [0,0,0,0]: return print(a) else: (recurs(a)) a = [1,2,3,4] recurs(a)
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message