Skip to main content

Section 5.32 Functions and Lists Write Code Questions

Checkpoint 5.32.1.

Write a function called average_of_num_list that takes in a parameter num_list and returns the average of all the numbers in num_list. For example, average_of_num_list([0, 20.8, 80, 5]) would return 26.45.
Solution.
Write a function called average_of_num_list that takes in a parameter num_list and returns the average of all the numbers in num_list. For example, average_of_num_list([0, 20.8, 80, 5]) would return 26.45.
def average_of_num_list(num_list):
    return sum(num_list) / len(num_list)

Checkpoint 5.32.2.

Write a function called names that takes in a parameter name_list and returns an alphabetically sorted name_list. For example, names(['Susan', 'Sara', 'Sammy', 'Sarah']) would return ['Sammy', 'Sara', 'Sarah', 'Susan'].

Checkpoint 5.32.3.

Write a function called remove_min_value that takes in a parameter num_list and returns a num_list without the minimum value from num_list. For example, remove_min_value([20, 4, 1203, 7482, 3]) would return [20, 4, 1203, 7482].
Solution.
Write a function called remove_min_value that takes in a parameter num_list and returns a num_list without the minimum value from num_list. For example, remove_min_value([20, 4, 1203, 7482, 3]) would return [20, 4, 1203, 7482].
def remove_min_value(num_list):
    num_list.remove(min(num_list))
    return num_list

Checkpoint 5.32.4.

Write a function called range_given_list that takes in a parameter list_of_nums and returns the range (max value - min value) of the values. Try using the sort method and indexing. For example range_given_list([20, 100, 2000, 15, 3, 12]) would return 1997.

Checkpoint 5.32.5.

Write a function called remove_indices_after_first_max_value that takes in a parameter num_list and returns a new_num_list with values up to the max value of the list. For example, remove_indices_after_first_max_value([200, 10, 5, 200]) would return [5, 10, 5, 200].
Solution.
Write a function called remove_indices_after_first_max_value that takes in a parameter num_list and returns a new_num_list with values up to the max value of the list. For example, remove_indices_after_first_max_value([200, 10, 5, 200]) would return [5, 10, 5, 200].
def remove_indices_after_first_max_value(num_list):
    index_value = num_list.index(max(num_list)) + 1
    new_num_list = num_list[:index_value]
    return new_num_list

Checkpoint 5.32.6.

Write a function called transform_and_combine that takes in two parameters, list_one, which must have at least one element, and list_two. Remove the last element from list_one, then reverse the list. Sort list_two, then extend list_two with list_one, and return list_two. Hint: Use list methods (e.g., pop, sort, append, reverse, and extend). For example, transform_and_combine([5, 20, 3, 15, 200, 0, 17], ['Hello', 'Bye', 'How are you?']) would return ['Bye', 'Hello', 'How are you?', 0, 200, 15, 3, 20, 5].