In: Computer Science
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5
sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5
sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2
in python
\
def sum_gt_avg(num_list):
total = 0 # this holds the result
# taking average of the list
avg = sum(num_list) / len(num_list)
# looping through each number
for one in num_list:
if one > avg: # number is greater than average
total += one
# returning total
return total
# sample runs
print(sum_gt_avg([1,2,3,4,5]))
print(sum_gt_avg([1,2,3,-4,5]))
print(sum_gt_avg([-1,-2,-3,-4,-5]))
# Hit the thumbs up if you are fine with the answer. Happy Learning!