Podziel funkcję na mniejsze
Inny inżynier z twojego zespołu napisał tę funkcję, która oblicza średnią i medianę posortowanej listy. Chcesz pokazać mu, jak podzielić ją na dwie prostsze funkcje: mean() i median()
def mean_and_median(values):
"""Get the mean and median of a sorted list of `values`
Args:
values (iterable of float): A list of numbers
Returns:
tuple (float, float): The mean and median
"""
mean = sum(values) / len(values)
values = sorted(values)
midpoint = int(len(values) / 2)
if len(values) % 2 == 0:
median = (values[midpoint - 1] + values[midpoint]) / 2
else:
median = values[midpoint]
return mean, median
To ćwiczenie jest częścią kursu
Pisanie funkcji w Pythonie
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
def mean(values):
"""Get the mean of a sorted list of values
Args:
values (iterable of float): A list of numbers
Returns:
float
"""
# Write the mean() function
____ = ____
return mean