+ 1

Can someone explain this code to me?

https://code.sololearn.com/cYJZo2Nt774i/?ref=app

13th Jun 2022, 2:10 PM
Radouane
Radouane - avatar
3 Respostas
+ 2
Haven’t really programmed in a while, but by playing around with the code I got the following: 1. The m() is your lambda function 2. The iteration in the lambda is a range of 3 (0,1,2), so by passing m(1) it returns 2, since 0*1 = 0, 1*1 = 1, and 2*1 = 2, which is the final value that the method returns. 3. Its printing “2” three times because the cm() method returns a lambda function that runs 3 times. 4. In a sense you could see it as a nested loop, where the lambda’s function’s for loop is nested inside the “for m in cm()” Again, this is my interpretation of the code. Hope this helps!
13th Jun 2022, 3:19 PM
Matias Dure
Matias Dure - avatar
+ 2
Matias Dure your interpretation is correct. Radouane using a lambda function like that kind of defeats its purpose. Their main advantage is not having to define a normal function. So defining a normal function that does nothing but return a lambda function is redundant.
13th Jun 2022, 4:07 PM
Simon Sauter
Simon Sauter - avatar
0
Thanks, I think I understand.
13th Jun 2022, 7:59 PM
Radouane
Radouane - avatar