0

can someone clearly explain this recursion ?

def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(17)) print(is_even(23))

4th Jul 2017, 9:48 PM
Edu Rut
Edu Rut  - avatar
6 Respuestas
+ 21
example is_even(3) -> is_odd(3-1)=is_odd(2) -> not is_even(2) -> not is_odd(2-1)=not is_odd(1) -> not (not is_even(1)) -> not(not is_odd(1-1))=not(not is_odd(0)) -> not(not(not is_even(0))) -> not(not(not True)))=not(not False)=not True=False another example is_even(2) -> is_odd(2-1)=is_odd(1) -> not is_even(1) -> not is_odd(1-1)=not is_odd(0) -> not(not is_even(0)) -> not(not True)=not False=True
5th Jul 2017, 12:45 AM
Ahmad Fahadh Ilyas
Ahmad Fahadh Ilyas - avatar
+ 2
If x is even(Numbers divisible by 2) return True if not call method is_odd... ...if x is odd return the reverse value of method is even(If returns true, it returns false, vice versa)
5th Jul 2017, 12:32 AM
Complex
Complex - avatar
+ 1
Excellent Ahmad thank you so much, @ahmad
5th Jul 2017, 2:07 AM
Edu Rut
Edu Rut  - avatar
0
in English, if x is zero, even; else what is_odd says of x-1. what is not even is odd. :) To see for yourself, insert print function calls at the first line of both these functions.
4th Jul 2017, 10:03 PM
Venkatesh Pitta
Venkatesh Pitta - avatar
0
explain clearly plz
4th Jul 2017, 10:32 PM
Edu Rut
Edu Rut  - avatar
0
If you follow me I will explain briefly. Follow me please. Let's start. def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(3)) is_odd is calling and it's value is 3. But is_even() is calling inside is_odd function. but it returns not value. I mean if the value in is_even true, it returns false. I think you understand me.
26th Nov 2022, 11:39 AM
ISMAEL YIMAM MOHAMMED
ISMAEL YIMAM MOHAMMED - avatar