[Tutor] sumHighest function help

cs at zip.com.au cs at zip.com.au
Fri Nov 4 21:20:05 EDT 2016


Interesting. Tracey Jones has a very similar question...
- Cameron Simpson <cs at zip.com.au>

On 04Nov2016 16:44, Lloyd Francis <lloydyf333 at gmail.com> wrote:
>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