g = lambda x:[lambda x:x*1, lambda x:x*x, lambda x:x*x*x, lambda x:42][x%4](x)
[g(x) for x in xrange(12)]
What is the next value of this sequence?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Did you try it?
Here is how each value is calculated:
Basically
g(x)calls one of the functions in the list withxas the argument, when called in a list comprehension withxrangeit will cycle through the functions, with every fourth call being the same function.I know that this is just an exercise to aid in understanding Python, but you should note that this is horribly inefficient code, as all four functions are recreated on each call to
g(). If you actually needed this behavior it would be better to just create a function withdefthat contains severalifstatements (which would make the code much more readable as well).