+ 5

[Solved] When we get a RecursionError in Python, statistically speaking, is it always a mistake to choose recursion?

I have a code that I think I have numerous problems with. The code has a recursive function. And apparently my iterable in the function argument which depends on input might exceed recursion depth. Upon search, I see that the recursion depth can be set to other values. But: If I have such an error, does this indicate that: I should have chosen some other approach than recursion knowing that the problem doesn't impose a limit on the number to be inputted? Or is it the case that I should have used some sort of generator instead of an iterable that's not lazy.(if that’s possible, sounds weird to me) I am not looking for solutions. No solutions. I am looking to gain familiarity for future self troubleshooting. Here's the example code: https://code.sololearn.com/cTmGCFktNghO/?ref=app (Edit: a last min change I'd made to the code made it output the same thing at all conditions. Sorry for that, got rid of it, thanks noteve) Thanks to all in advance.

25th Jul 2022, 6:22 AM
Korkunç el Gato
Korkunç el Gato - avatar
5 Respostas
+ 4
At a glance I see that the function uses tail recursion - that is to say it recurses at the tail end of the routine. This pattern of recursion is very easy to convert into an iterative solution, and that is what I would recommend in order to save time and memory - especially since the stack is filling up.
25th Jul 2022, 7:37 AM
Brian
Brian - avatar
+ 4
Korkunç el Gato regarding any solutions that are recursion-worthy and require depth extension, I do not know. Whenever I hit that limit, I consider it a sign that there is bad logic in my recursion. Or else it is time to optimize or use memoization. Your summary of what you found on SO shows a good grasp of the issues. Recursion can be inevitable when you are running out of development time, and it might take you longer to convert into iteration. Another reason might be if you have limited program space, say in firmware, often a recursive technique takes less code.
25th Jul 2022, 1:17 PM
Brian
Brian - avatar
+ 3
Brian Thank you I think that singlehandedly answers my question. Out of curiosity though: Are there situations where recursion seems the most useful, but depth setting is the problem? Someone on SO(edit: not SL), as an answer to a recursion error question, mentioned a code for changing depth setting. When is that ever advisable?
25th Jul 2022, 7:43 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Brian I am sorry, my questions have no end but according to this site: https://www.geeksforgeeks.org/tail-recursion/ 1. tail recursion is the better recursion in terms of stack and memory in comparison to nontail. 2. Combining that with what you said, they are also the functions that are easily avoidable (unless I misunderstand what you said by "iterative solution") So then all this reads like, it's best to use the worst recursion, the nontail recursion, and only that because recursion must be used when it's inevitable? (Although I wouldn't know what inevitable encapsulates) (What a mentor says and what a noob understands 🙃)
25th Jul 2022, 8:01 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Brian Thank you for clearing up each and every thing. Great to know all this.
25th Jul 2022, 3:23 PM
Korkunç el Gato
Korkunç el Gato - avatar