+ 5
How this code works, can anybody help me to understand this code.
The code is- 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(2)) print(is_even(1)) print (is_odd(1)) print (is_even(2)) Output:- False False True True https://code.sololearn.com/cvOzs768zjjt/?ref=app Help me to understand the working of that code/program.
24 Antworten
+ 9
ミ Shiva 彡
These functions are working by calling themself one by one. And lastly it ends on is_even() function with a zero value.
if we pass 2 to a is_even() function,
is_even(2)=>return is_odd(1)=>return not is_even(1)=>return is_odd(0)=>return not is_even(0)=>return True
So,
is_even(2)=>(return not(return not(return True)))
return(not(not(True)))
return (not(False))=True
if we pass 3 to a is_odd() function,
is_odd(3)=>return not is_even(3)=>return is_odd(2)=>return not is_even(2)=>return is_odd (1)=>return not is_even(1)=>return is_odd(0)=>return not is_even(0)=>return True
So,
is_odd(3) =>return(not(not(not(not(True))))) =return(True)=True
+ 7
Try thinking through what happens with a call to each of the functions with the smallest even (2) and odd (1) numbers and then apply that to any other even or odd call to those functions as they will have the same results.
is_even(1)
is_odd(1)
is_even(2)
is_even(2)
+ 4
ミ Shiva 彡 ,
you don't need 2 functions to check for even or odd. use one function, pass a number as argument to it, and the function will return True or False :
def is_even(x):
if x%2 == 0:
return True
else:
return False
print(is_even(2))
print(is_even(1))
if you like it the short way, you can replace the whole function like this:
def is_even(x):
return True if x%2==0 else False
+ 3
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ Ok and
Thanks for giving your precious time
+ 1
ChaoticDawg Can you explain it in brief
+ 1
ミ Shiva 彡
Yeah, I forget to type that. Now, I corrected see that 👆.
+ 1
# Lothar the shortest way (wich return True or False) and most efficient way would be:
is_even = lambda x: x&1==0
# and returning 1 or 0 is even shortest:
is_even = lambda x: x&1^1
is_odd = lambda x: x&1
+ 1
Lothar
Thanks for suggesting
But i don't want a simpler or easy way of knowing even or odd number,
I want to know how the program works.
Again thanks 😊
I agree with you
+ 1
visph Thanks you for giving me a simpler way
I agree with you
But i want to know the working of program not any simple way.
Again, thanks 😊
+ 1
The number in perinthesis in
print(is_odd(1)) translates to x in the is_odd(x): function
And when print(is_odd(2)) runs, it will first read the is_odd(x) function and when it calls is_even(x) in the line:
“return not is_even(x)”, it will also return the opposite bool, so when x, or 1 in this case, is subtracted by one in the line
return is_odd(x-1)
It will go back to the is_odd(x) function and that will make it go back, but now, if is_even(x) is true, it will return true since the odd function said not twice, a double negative, which results in a positive so when it says x = 0, and it returns true, it will print true. If the print() said instead
print(is_even(1))
It would start with is_even(x) and x would become 0, and when it comes back from the is_odd function, it will return the opposite boolean (true/false) and we would get a false as our output, hope this helped!
+ 1
Ajay Parimi Please don't spam.
+ 1
Carolina Humphrey Please keep your comments 13+. Your comment has been reported. It's not acceptable to post any NSFW content in a coding platform. SoloLearn is not Tinder.
0
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ As you say-
is_even(2) gives a output of False
But it's not correct it will give output of True
0
ミ Shiva 彡 Here are the one-liner lambda functions:
is_even, is_odd = lambda x: not x & 1, lambda x: x & 1
print(is_even(78))
print(is_odd(78))
# Hope this helps
- 2
Odd
- 2
U no some off coding to learn easy
- 3
Hello
- 3
Hi
- 3
I want to learn coding easy in mobile