+ 1
Can anyone explain me how this code works
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2465
5 Antworten
+ 1
Airree is_even always hits to zero and is_odd always returns False. the result is determined by the huge amount of not's that accumulate during the recursion. eg not not not not not... False=True/False
edit: in general, if there are odd number of not's the number is evaluated to be even ie is_even returns True. for even number of not's the number is even.
+ 2
Let's say it is
factorial(3)
it returns 3 * factorial(3 - 2)
so 3 * 2 * factorial (2 - 1), and when n equals 1, it just returns 1 -> 3 * 2 * 1 == 6,
so it just returns 1 * 2 * ... * n
+ 2
Okay, so if a number is bigger than 0, for example 3, then first it returns is_odd(3 - 1), which returns not is_even(2 - 1), and that goes down to 0. So if it hits 0 when it runs with the is_odd, it should return true, but it's not True, so false, if it normally hits 0 (with is_even) it returns true
+ 1
let x=3, then the code goes like this:
3*fac(2)
=3*2*fac(1)
=3*2*1
the f() returns 1 for x=1 and ends the recursion there. try to grasp on this example for a moment.
0
I want explanation for the code in 3rd page
i.e. In the last page of recursion chapter.
For the is even and is odd code.