A murder caused by a python face test

Source: Internet
Author: User

Here is a Ali School recruit interview question: A line of code to implement the list A in the even position of the elements to add 3 after summing?

  Today went to interview also encountered this topic, this question examines the Python high-order function Map/filter Flexible use (specific use method can refer to the "official website of Liao Xuefeng"). As a small white my use of higher-order functions is not much, at that time even the name of the higher-order function is not clear (the book to when the party hated less), the duly abused a no way to go. Helpless write down the topic back to the degree Niang, did not think is Ali's school recruit topic, on-line also gave the answer, but it is obvious that the answer is there are some problems, specific what problem in here do not talk about, we can find themselves, and find out the problem.

Since I can't remember the shape of List A, let's first define a list A = [2, 5, 7, 9, 34, 12]

First, we're going to find the element in the even position of List A, where we're going to use the filter function (the specific use of filter does not speak here):

1 r = Filter (lambda u:a.index (u)% 2 = = 1, a)  # in order to get the index value of the element in the list,
Call List.index (). 2 print (list (r)) # results are: [5, 9, 12]

 Once you have the element in the even position, use the map function to add 3 to each element:

1 s = map (Lambda y:y+3, List (R))2print(list (s))                      # results are: [8, 12, 15 ]

  So we add 3 to the element of the dual position, and here the SUM function is the simplest, and finally the complete code is put on:

SUM (list (map (Lambda y:y+3, filter (lambda u:a.index (u)% 2 = = 1, a)))   #结果: 35

The above line of code can already solve the problem, in addition we can call another high-order function to complete the summation, yes, is the reduce function, before using the reduce function, the first call:

1  from Import reduce    # Of course, this violates a single line of code to solve the problem.  Reduce (lambda x, y:x+y, map (Lambda y:y+3, filter (Lambda u:a.index (U) %2 = = 1, a)))    #结果: 35

 Well, speaking of this, even if the topic is finished, if you have a more simple way, also please list in the message, we communicate with each other in order to progress together.

What, you ask me where the murder is, of course, ignore and forget it!!!

A murder caused by a python face test

Related Article

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.