Given a list of numbers, return a new list consists of the number which is larger than the sum of the remaining numbers in a list or larger than absolute zero. For example, this list [2, 5, 2, 1] will convert to [5, 2, 1] because 5 is greater than 2+1, 2 is greater than 1 and 1 is greater than absolute zero. Another example. [2, 3, -3] will convert to [2, 3] because 2 is greater than 3+(-3) and 3 is greater than -3 but -3 is lesser than 0 which means -3 will not get included into the new list.

```def array_leaders(numbers):
arr = [] # the new list to store the leading numbers
while(len(numbers)>0):
# return the array if there is nothing to sum up
if(len(numbers) == 0 and leader > 0):
return arr
elif(len(numbers) == 0):
return arr

sum = 0
for elem in numbers:
sum+=elem #add up all other numbers
arr.append(leader) # if the leading number is larger than the total then append that number to the number leader list
```

Click to rate this post!
[Total: 0 Average: 0]

1. anonymous says:

if len(numbers) == 0:
return []
else:
if numbers > sum(numbers[1:]):
else:

2. Michael Beale says:

a = [2,5,2,1]
[x for i,x in enumerate(a) if x > sum(a[i+1:])]
>>> [5,2,1]

3. Nathan says:

Here’s a way with cumulative summation in one step, then using that as a filter in another step. Not sure how it measures up in efficiency, and readability is usually subjective.

from itertools import accumulate