[Tutor] sumHighest function help

Lloyd Francis lloydyf333 at gmail.com
Fri Nov 4 12:44:11 EDT 2016


Hi,

I want to write a function that will calculate and return the sum of the *n*
highest value in a list *a. *Also, when n is less than 0, the answer should
be zero, and if n is greater than the number of elements in the list, all
elements should be included in the sum.

In addition i want to write the code in a function with only one line of
code in it.
So far I have:-

def sumHighest(a, n):
lambda a, n : sum(a[:n])

This doesn't work but i want to use *lambda *and the slice in the function.

An appropriate test would be:-

input - sumHighest([1,2,3,4], 2)
output - 7

thanks L.


More information about the Tutor mailing list