""" Rename this file from list_op_shell.py to list_op.py and write the function bodies below. """ def average(num_list): """ Returns the average of numbers in a list. Returns 0 for an empty list. Replace "pass" with your code below and remove this comment line. """ # # list_1 = [1,3,5,9,8,7,1,1] # # code for the average of list_1 # # step by step algorithm # average = sum of the list / number of elements in the list. # list_1 average: 35 / 8 = 4.375 # # sum of the list. num_sum = sum(list_1) print(num_sum) # number of elements in the list. print(list_1.count(1)) print(list_1.count(4)) print(list_1.count(7)) print(len(list_1)) list_2 = [] print(sum(list_2)) print(len(list_2)) def average(num_list): """ Returns the average of numbers in a list. Returns 0 for an empty list. Replace "pass" with your code below and remove this comment line. """ num_sum = sum(num_list) num_count = len(num_list) if num_count == 0: # one way to handle division by Zero return None # missing information. else: return num_sum / num_count def average2(num_list): """ Returns the average of numbers in a list. Returns 0 for an empty list. Replace "pass" with your code below and remove this comment line. """ return sum(num_list) / len(num_list) print(f"average(list_1): {average(list_1)}") print(f"average2(list_1): {average2(list_1)}") print(f"average(list_2): {average(list_2)}") # print(f"average2(list_2): {average2(list_2)}") def count_greater_occurrences(num_list, threshold): """ Counts the occurrences of elements in a list that is greater than a specific threshold. Replace "pass" with your code below and remove this comment line. """ list_a = [1,5,3,7,6,4,7,5] threshold_a = 4 # result: 5 # algorithm: # count <- 0 # loop through element e in list_a: # if e > threshold_a # count <- count + 1 # return count count = 0 for item in list_a: if item > threshold_a: count += 1 print(f"count of elements in list_a that is greater than 4: {count}") list_b = [1,1,2,3,-9,0] threshold_b =4 count = 0 for item in list_b: if item > threshold_b: count += 1 print(f"count of elements in list_b that is greater than 4: {count}")