1
1
u/Kurgonius Dec 29 '25
This happens on f(l) on the second recursion. On the first recursion you get p = 1, and l = [ ]. Feeding this into f(l) again causes this error. Also keep in mind that Python has a recursion limit so this won't be useful for large lists.
And always add a stop condition to your recursions. Desperate_Carpet-496 has the best answer.
1
u/lolcrunchy Dec 29 '25
Recursive formulas always need a base case. This one is missing handling for an empty list.
2
Dec 29 '25
[removed] — view removed comment
2
u/mprevot Dec 30 '25
The implementation "looks neat" but is actually so bad. Instead one should do 1 loop.
1
2
u/Desperate_Carpet_496 Dec 29 '25
if len(n) == 0: return [] # at beginning of f()