Why is the output 222? A question from challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is the output 222? A question from challenge

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

20th Aug 2019, 4:06 AM
你知道規則,我也是
你知道規則,我也是 - avatar
2 Answers
+ 3
https://www.sololearn.com/discuss/1265773/?ref=app https://www.sololearn.com/discuss/1267956/?ref=app
20th Aug 2019, 5:14 AM
Diego
Diego - avatar
0
As already said in suggested threads, the output should be the result of three lambdas, invoked with the same argument 1, and factorized by a parameter i which is expected different for each of the three lambdas, respectively 0 1 2. So the expected result should be 012 instead of 222. Why this? Because a list is evaluated only when it is used, and not when it is being built: the parameter i, at the moment of the list use, has the same final value 2 for each of the three lambdas. To better understand this, I tried to code some examples using simpler lambdas without arguments. Examples (1) and (2) show this unexpected - maybe unwanted - behaviour. All the other examples are possible alternatives to obtain the normal behaviour, including generator expressions and generator functions. https://code.sololearn.com/cXvSlXFaDG8b/?ref=app
20th Aug 2019, 9:53 PM
Bilbo Baggins
Bilbo Baggins - avatar