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))
6 Respostas
+ 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
+ 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)
+ 1
Excellent Ahmad thank you so much, @ahmad
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.
0
explain clearly plz
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.