E X E R C I S E # 1 5 : M E D I A N
median([3, 7, 10, 4, 1, 6, 9, 2, 8])
→
6
If you put a list of numbers into sorted order, the median number is the number at the halfway
point. Outliers can cause the statistical average to be much higher or smaller than the majority of
numbers, so that the median number may give you a better idea of the characteristics of the numbers
in the list. This, the previous, and the next exercise challenge you to make Python solve these statistics
calculations.
Exercise Description
Write a median() function that has a numbers parameter. This function returns the statistical
median of the numbers list. The median of an odd-length list is the number in the middlemost
number when the list is in sorted order. If the list has an even length, the median is the average of the
two middlemost numbers when the list is in sorted order. Feel free to use Python’s built-in sort()
method to sort the numbers list.
Passing an empty list to median() should cause it to return None.
These Python assert statements stop the program if their condition is False. Copy them to
the bottom of your solution program. Your solution is correct if the following assert statements’
conditions are all True:
assert median([]) == None
assert median([1, 2, 3]) == 2
assert median([3, 7, 10, 4, 1, 9, 6, 5, 2, 8]) == 5.5
assert median([3, 7, 10, 4, 1, 9, 6, 2, 8]) == 6
import random
random.seed(42)
testData = [3, 7, 10, 4, 1, 9, 6, 2, 8]
for i in range(1000):
random.shuffle(testData)
assert median(testData) == 6
Shuffling the order of the numbers should not affect the median. The for loop does 1,000 such
random shuffles to thoroughly check that this fact remains true. For an explanation of the
random.seed()
function, see the Further Reading section of Exercise #19, ―Password
Generator‖.
Try to write a solution based on the information in this description. If you still have trouble
Python Programming Exercises, Gently Explained
47
solving this exercise, read the Solution Design and Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: len(), for loops, augmented assignment operators, integer division,
modulo operator, indexes
Dostları ilə paylaş: |