Re: [Tutor] sumHighest function help

2016-11-05 Thread Alan Gauld via Tutor
On 05/11/16 12:07, Peter Otten wrote: > sum_highest = lambda items, n: sum(sorted(items, reverse=True)[:max(n, 0)]) > > or better: > > import heapq > > def sum_highest(items, n): > return sum(heapq.nlargest(n, items)) No, the first solution is "better" because it used lambda and slicing a

Re: [Tutor] sumHighest function help

2016-11-05 Thread Peter Otten
Lloyd Francis wrote: > 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. >

Re: [Tutor] sumHighest function help

2016-11-04 Thread cs
Interesting. Tracey Jones has a very similar question... - Cameron Simpson On 04Nov2016 16:44, Lloyd Francis 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

Re: [Tutor] sumHighest function help

2016-11-04 Thread Alan Gauld via Tutor
On 04/11/16 16:44, Lloyd Francis wrote: It looks suspiciously like you posted this same message from two addresses with different subjects, please don't do that as it splits the responses and makes searching archives more difficult. > I want to write a function that will calculate and return the

[Tutor] sumHighest function help

2016-11-04 Thread Lloyd Francis
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 writ