+ 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
6 Answers
+ 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
+ 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".
+ 3
Am not now
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
Â
Â
)
0
Answr?
0
__
_
__egg