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): leader = numbers.pop(0) # return the array if there is nothing to sum up if(len(numbers) == 0 and leader > 0): arr.append(leader) return arr elif(len(numbers) == 0): return arr sum = 0 for elem in numbers: sum+=elem #add up all other numbers if leader > sum: arr.append(leader) # if the leading number is larger than the total then append that number to the number leader list
If you have a better solution then please provide your answer in the comment box below.