+ 5

Anyone can explain this code functionality?

e= lambda x:True if x==0 else o(x-1) o= lambda x: not e(x) print( "even" if e(10//2) else "odd") Output is odd

16th Aug 2019, 3:22 AM
Maduchandan B P
6 ответов
+ 13
e= lambda x:True if x==0 else o(x-1) o= lambda x: not e(x) print( "even" if e(10//2) else "odd") e(5) not e(4) not(not e(3)) not(not(not e(2))) not(not(not(not e(1)))) not(not(not(not(not e(0))))) not(not(not(not(not e(True))))) 5 not is the same 1 not, and we have not True I.e. False
16th Aug 2019, 3:57 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 6
It is called mutual recursion. The lambda functions call each other until the base case in one of the functions is reached. In this case the condition for the base case is zero and "True" is returned by the base case. Then it depends on how often the "negating" function has been called during the recursion phase. Michail has nicely demonstrated the logic behind it (see above). If the negating function (o) has been called an odd number of times, the "True" (which has been returned by the base case) will always swich to "False" in the end so that the ternary expression in the last line will return "odd".
16th Aug 2019, 12:59 PM
Thoq!
Thoq! - avatar
+ 3
Am not now
16th Aug 2019, 9:21 AM
MDSaiff
MDSaiff - avatar
0
Fill in the blanks to make the egg attribute strongly private and access it from outside of the class. class Test:        egg = 7 t = Test() print(t.     Test     )
16th Aug 2019, 3:55 PM
SALEENA BEEVI S N
SALEENA BEEVI S N - avatar
0
Answr?
16th Aug 2019, 3:55 PM
SALEENA BEEVI S N
SALEENA BEEVI S N - avatar
0
__ _ __egg
19th Aug 2019, 9:17 PM
Ankit Tyagi
Ankit Tyagi - avatar