Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:58:41+00:00 2026-06-15T15:58:41+00:00

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

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:58:43+00:00Added an answer on June 15, 2026 at 3:58 pm

    Did you try it?

    >>> 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)]
    [0, 1, 8, 42, 4, 25, 216, 42, 8, 81, 1000, 42]
    

    Here is how each value is calculated:

    [
     0,     # x is 0, x%4 is 0, so g(x) becomes (lambda x:x*1)(0) or 0*1
     1,     # x is 1, x%4 is 1, so g(x) becomes (lambda x:x*x)(1) or 1*1
     8,     # x is 2, x%4 is 2, so g(x) becomes (lambda x:x*x*x)(2) or 2*2*2
     42,    # x is 3, x%4 is 3, so g(x) becomes (lambda x:42)(3) or 42
     4,     # x is 4, x%4 is 0, so g(x) becomes (lambda x:x*1)(4) or 4*1
     25,    # x is 5, x%4 is 1, so g(x) becomes (lambda x:x*x)(5) or 5*5
     216,   # x is 6, x%4 is 2, so g(x) becomes (lambda x:x*x*x)(6) or 6*6*6
     42,    # x is 7, x%4 is 3, so g(x) becomes (lambda x:42)(7) or 42
     8,     # x is 8, x%4 is 0, so g(x) becomes (lambda x:x*1)(8) or 8*1
     81,    # x is 9, x%4 is 1, so g(x) becomes (lambda x:x*x)(9) or 9*9
     1000,  # x is 10, x%4 is 2, so g(x) becomes (lambda x:x*x*x)(10) or 10*10*10
     42     # x is 11, x%4 is 3, so g(x) becomes (lambda x:42)(11) or 42
    ]
    

    Basically g(x) calls one of the functions in the list with x as the argument, when called in a list comprehension with xrange it 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 with def that contains several if statements (which would make the code much more readable as well).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is this 'Lambda' everyone keeps speaking of? A lot of people seem to
Why does this lambda expression not compile? Action a = () => throw new
This lambda does not compile, but I do not understand why. using System; using
This LINQ and lambda expressions are killing me, so there is me again searching
Lambda expression for Contains operator I am able to generate using this code. Expression
Can lambda expressions be used as class template parameters? (Note this is a very
lambda is used in this example in both the compose and the hydrate methods.
(lambda (head . rest) (...)) I encountered this code on the net when trying
Using lambda's in VB.Net results in no intellisense. Is this a bug with VS2010
In Lambda calculus, Y -combinator returns itself like this Y a = a Y

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.