Python lambda function to calculate factorial of a number

前端 未结 11 1255
误落风尘
误落风尘 2020-12-30 12:00

I have just started learning python. I came across lambda functions. On one of the problems, the author asked to write a one liner lambda function for factorial of a number.

相关标签:
11条回答
  • 2020-12-30 12:38

    We can use the below lambda expression

         fact = lambda n:1 if n==0 else n*fact(n-1)
         print(fact(5)
         >>> 120
    
    0 讨论(0)
  • 2020-12-30 12:43

    There are two hard parts about this function.
    1. lambda a, b: b*a(a, b-1) if b > 0 else 1.
    2. the "b" that's folowing 1.

    For 1, it's nothing more than:

    def f(a, b):
        if b > 0:
            b * a(a, b - 1)
        else:
            1
    

    For 2, this b

    (lambda b: (lambda a, b: a(a, b))(lambda a, b: b*a(a, b-1) if b > 0 else 1,b))(num)
                                                                          (this one)
    

    is actually this b:

    (lambda b: (lambda a, b: a(a, b))(lambda a, b: b*a(a, b-1) if b > 0 else 1,b))(num)
       (this one)
    

    The reason is that it's not inside the definition of the second and third lambda, so it refers to the first b.

    After we apply num and strip off the outer function:

    (lambda a, b: a(a, b))  (lambda a, b: b*a(a, b-1) if b > 0 else 1, num) 
    

    It's just applying a function to a tuple, (lambda a, b: b*a(a, b-1) if b > 0 else 1, num)
    Let's call this tuple as (f, num) (f's def is above) Applying lambda a, b: a(a, b) on it, we get

    f(f, num).

    Suppose your num is 5.
    By definiton of f, it first evaluates to

    5 * f(f, 4)  
    

    Then to:

    5 * (4 * f(f, 3)) 
    

    All the way down to

    5 * (4 * (3 * (2 * (1 * f(f, 0)))))
    

    f(f, 0) goes to 1.

    5 * (4 * (3 * (2 * (1 * 1))))
    

    Here we go, the factorial of 5.

    0 讨论(0)
  • 2020-12-30 12:44

    the generalized def of recursive lambda is as below

    recursive_lambda = (lambda func: lambda *args: func(func, *args))
    
    0 讨论(0)
  • 2020-12-30 12:46

    It is this simple:

    n=input()
    
    print reduce(lambda x,y:x*y,range(1,n+1))
    
    0 讨论(0)
  • 2020-12-30 12:47

    The factorial itself is almost as you'd expect it. You infer that the a is... the factorial function. b is the actual parameter.

    <factorial> = lambda a, b: b*a(a, b-1) if b > 0 else 1
    

    This bit is the application of the factorial:

    <factorial-application> = (lambda a, b: a(a, b))(<factorial>, b)
    

    a is the factorial function itself. It takes itself as its first argument, and the evaluation point as the second. This can be generalized to recursive_lambda as long as you don't mind a(a, b - 1) instead of a(b - 1):

    recursive_lambda = (lambda func: lambda *args: func(func, *args))
    print(recursive_lambda(lambda self, x: x * self(self, x - 1) if x > 0 else 1)(6))
    # Or, using the function verbatim:
    print(recursive_lambda(lambda a, b: b*a(a, b-1) if b > 0 else 1)(6))
    

    So we have the outer part:

    (lambda b: <factorial-application>)(num)
    

    As you see all the caller has to pass is the evaluation point.


    If you actually wanted to have a recursive lambda, you could just name the lambda:

    fact = lambda x: 1 if x == 0 else x * fact(x-1)
    

    If not, you can use a simple helper function. You'll notice that ret is a lambda that can refer to itself, unlike in the previous code where no lambda could refer to itself.

    def recursive_lambda(func):
        def ret(*args):
            return func(ret, *args)
        return ret
    
    print(recursive_lambda(lambda factorial, x: x * factorial(x - 1) if x > 1 else 1)(6))  # 720
    

    Both ways you don't have to resort to ridiculous means of passing the lambda to itself.

    0 讨论(0)
  • 2020-12-30 12:53

    if you want to do a factorial using lambda expression then you need to put if else condition like:

    if x==1: 
        return 1 
    else 
        return x*function_name(x-1)
    

    for example fact is my lambda expression:

    fact( lambda x:1 if x==1/else x*fact(x-1) )

    this is recursive if else condition

    0 讨论(0)
提交回复
热议问题