Home > Backend Development > Python Tutorial > Example tutorial on Lambda expressions

Example tutorial on Lambda expressions

PHP中文网
Release: 2017-06-20 15:01:14
Original
1834 people have browsed it

Five exercises for Lambda expressions

Syntax:

Lambda function is an anonymous function, creation syntax:

lambda parameters:express

parameters: Optional, if provided, usually in the form of comma-separated variable expressions, that is, positional parameters.

expression: cannot contain branches or loops (but conditional expressions are allowed), nor can it contain return (or yield) functions. If it is a tuple, it should be enclosed by parentheses .

Call the lambda function, and the result returned is the result of the expression calculation.

  • 1. Question: Use the nesting of conditional operators to complete this question: Students with academic scores >= 90 points are represented by A, between 60-89 points Those with a score of 60 or less are represented by C.

##print( str(score) + ' belongs to ' +

(lambda x:(x>=90 and 'A' or x>= 60 and 'B' or 'C'))(score))

2. Topic: Recursive call of lambda. Split an integer n and print it out. For example, 234 should be printed in three lines: 2, 3, and 4.

n = int(input ())
f=m,f:( (m / 10) !=0 f( int(m/10),f) ( m!=0  print(%(m%10) )))
f(n,f)
Copy after login

3. Question: Decompose a positive integer into prime factors. For example: input 90, print out 90=2*3*3*5.

Program analysis: To decompose n into prime factors, you should first find a minimum prime number k, and then complete it according to the following steps:

(1) If this prime number is exactly equal to n , it means that the process of decomposing prime factors has ended, just print it out.

(2) If n<>k, but n is divisible by k, the value of k should be printed out, and the quotient of n divided by k should be used as the new positive integer n,

Repeat the first step.

(3) If n is not divisible by k, use k+1 as the value of k and repeat the first step.

import os

import sys
from functools import reduce
from math import sqrt

n = int(input ("Please input a int:\n"))
ans =[]
nn=n
f=lambda m,i,c,f:(m%i ==0 and f(m//i,i,c+1,f) or (m,c))
def func(x,y):
ret,count=f(x,y,0,f )
if count >0:
ans.append(count * [y] )
return ret
reduce(func,[nn]+list(range(2,nn)))
print(ans)

4. Question: Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a number. For example, 2+22+222+2222+22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the input.


n= int(input("n=:\n"))
aa =a= int(input('a= :\n'))

f=lambda n,a,ans,la,f: n==0 and ans or f(n-1,a,ans+[la* 10+a],la*10+a,f )

print(reduce(lambda x,y:x+y,f(n,aa,[],0,f )))

  • 5. Question: If a number is exactly equal to the sum of its factors, the number is called a "perfect number" ". For example, 6=1+2+3.

Programming: Find all perfect numbers within 1000.

                                                                                                                   

factors = lambda x:filter( lambda i: x%i==0 and i,range(1,x))
f = lambda x: sum(factors(x)) == x
print( [(i,list(factors(i))) for i in list(filter( f, range(2,1001)))])

The above is the detailed content of Example tutorial on Lambda expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template