Five Lambda expressions and five lambda expressions

Source: Internet
Author: User

Five Lambda expressions and five lambda expressions
Lambda expressions

Syntax:

Lambda function isAnonymous Functions, Create Syntax:

Lambda parameters: express

Parameters: Optional. If provided, it is usually a comma-separated variable expression form, that is, the location parameter.

Expression: it cannot contain branches or loops (but canConditional expressions), And cannot contain the return (or yield) function. IfTuples, Then the applicationParenthesesInclude it.

When the lambda function is called, the returned result isResult of expression Calculation.

 

  • 1. question: Use nested conditional operators to complete this question: Students with scores> = 90 are represented by A, students with scores between 60 and 89 are represented by B, and students with scores below 60 are represented by C.

 

Print (str (score) + 'belongs to '+

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

 

2. Question: recursive call of lambda. Split and print an integer n. For example, 234 rows should be printed in 3, 3, and 4 rows.

 

n = int(input ("Please input a int:\n"))
f=lambda m,f:( (m / 10) !=0 and f( int(m/10),f) or  ( m!=0  and print("%d"  %(m%10) )))
f(n,f)

 

3. Question: decompose a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5.

Program Analysis: to break down the prime factor of n, first find a minimum prime number k, and then follow the steps below:

(1) If the prime number is equal to n, it indicates that the process of decomposing the prime factor is over. Print it out.

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

Repeat the first step.

(3) If n cannot be divisible by k, k + 1 is used as the value of k and the first step is repeated.

 

Import OS
Import sys
From functools importreduce
From math import sqrt

N = int (input ("Pleaseinput a int: \ n "))
Ans = []
Nn = n
F = lambda m, I, c, f :( m % I = 0and 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: calculate 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). 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 equal to the sum of its factors, it is called the "complete number ". For example, 6 = 1 + 2 + 3.

Programming: Find all the final 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)])

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.