In this lab you will be able to work with a partner, but each person must complete the prelab independently.
Review the class notes and examples from this week covering lists. In particular, recall there are built-in functions that apply to lists:
len(l)Returns the number of items in list
max(l)Returns the maximum value in list
min(l)Returns the minimum value in list
sum(l)Returns the sum of the values in list
sorted(l)Returns a copy of list
lwhere the items are in order from smallest to largest (this does not mutate
Furthermore, the Python documentation for lists includes links to operations and methods that apply to common sequence types and mutable sequence types including lists.
Answer these two questions on Gradescope
Question 1: Write a Python expression that sorts a list
data in place. For example, if
data were the list
[45, 6.2, -3.5, 10] then after execution of your expression the list
data would contain
[-3.5, 6.2, 10, 45].
Question 2: Write a sequence of Python statements that given a non-empty list would return the middle value (not median!) in the list. For an odd number of list elements, this is the value at the middle position. For an even number of list elements, return the average of the two middle items. For example, if
data=[35, 14, 10, 81, 40] then
middle(data) would evaluate to 10. If
data=[35, 14, 10, 81] then
middle(data) would evaluate to 12, the average of 14 and 10. Your statements should complete the following function:
def middle(sequence): """ Return middle value in non-empty sequence. Args: sequence: a sequence of values of any type, sorted or unsorted Return: The middle element if sequence has odd length, or the average of the two middle elements if sequence has even length """