Here is how to write a recursion while avoiding stack overflow
Run the code here: https://repl.it/@VinitKhandelwal/recursion
1
2
3
4
done
Run the code here: https://repl.it/@VinitKhandelwal/recursion
counter = 0
def inception():
# To avoid recursion
global counter
print(counter)
if counter > 3:
return "done"
counter += 1
# recursion
return inception()
print(inception())
OUTPUT
01
2
3
4
done
Comments
Post a Comment