+ 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.

30th Jun 2021, 5:48 AM
Sagar Yadav
Sagar Yadav - avatar
24 odpowiedzi
+ 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
30th Jun 2021, 6:39 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 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)
30th Jun 2021, 6:18 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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
30th Jun 2021, 10:40 AM
Lothar
Lothar - avatar
+ 3
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ Ok and Thanks for giving your precious time
30th Jun 2021, 8:01 AM
Sagar Yadav
Sagar Yadav - avatar
+ 3
ミ Shiva 彡 I know: I was only replying to Lothar ;P
30th Jun 2021, 1:59 PM
visph
visph - avatar
+ 1
ChaoticDawg Can you explain it in brief
30th Jun 2021, 6:25 AM
Sagar Yadav
Sagar Yadav - avatar
+ 1
ミ Shiva 彡 Yeah, I forget to type that. Now, I corrected see that 👆.
30th Jun 2021, 7:59 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 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
30th Jun 2021, 1:37 PM
visph
visph - avatar
+ 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
30th Jun 2021, 1:55 PM
Sagar Yadav
Sagar Yadav - avatar
+ 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 😊
30th Jun 2021, 1:58 PM
Sagar Yadav
Sagar Yadav - avatar
+ 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!
1st Jul 2021, 8:15 PM
Spiron
+ 1
Ajay Parimi Please don't spam.
2nd Jul 2021, 4:23 AM
Calvin Thomas
Calvin Thomas - avatar
+ 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.
2nd Jul 2021, 6:01 AM
Calvin Thomas
Calvin Thomas - avatar
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
30th Jun 2021, 7:03 AM
Sagar Yadav
Sagar Yadav - avatar
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
1st Jul 2021, 12:06 PM
Calvin Thomas
Calvin Thomas - avatar
- 2
Odd
1st Jul 2021, 6:02 PM
Bharathi T
- 2
U no some off coding to learn easy
2nd Jul 2021, 4:00 AM
Ajay Parimi
- 3
Hello
1st Jul 2021, 7:58 PM
Adres Barwary
Adres Barwary - avatar
- 3
Hi
2nd Jul 2021, 3:59 AM
Ajay Parimi
- 3
I want to learn coding easy in mobile
2nd Jul 2021, 3:59 AM
Ajay Parimi