Can you write the function in python?
def remove_outliers(data, num_outliers):
# When analyzing data collected as a part of a science experiment
it
# may be desriable to remove the most extreme values before
performing
# other calculations. Complete this function which takes a list
of
# values and an non-negative integer, num_outliers, as its
parameters.
# The function should create a new copy of the list with the
num_outliers
# largest elements and the num_outliers smallest elements
removed.
# Then it should return teh new copy of the list as the function's
only
# result. The order of the elements in the returned list does not
have to
# match the order of the lemetns in the original list.
# input1: data (list)
# input2: num_outliers (int)
# output: list
It has to pass this test.
import random
random.seed(1234)
data = [random.randint(50, 150) for ele in range(100)]
data[45] = 1
data[46] = 2
data[90] = 250
data[34] = 300
result = [50, 50, 51, 52, 52, 52, 53, 53, 54,
55, 55, 55, 55, 56, 58, 58, 59, 59, 59, 60, 61, 61,
61, 62, 64, 64, 64, 68, 68, 68, 69, 69, 69, 70, 71,
72, 73, 75, 77, 80, 81, 81, 84, 84, 84, 85, 88, 88, 89,
92, 94, 94, 95, 95, 98, 103, 106, 108, 108, 109, 109, 109,
110, 111, 111, 112, 113, 114, 115, 115, 117, 117, 119, 121,
124, 124, 125, 126, 128, 129, 132, 132, 133, 134, 135, 135,
135, 136, 138, 140, 141, 148, 148, 149, 149, 150]
assert remove_outliers(data, 2) == result
def remove_outliers(data, num_outliers):
copy = data[:]
copy=sorted(copy)
copy=copy[num_outliers:len(data)-num_outliers]
return copy
Get Answers For Free
Most questions answered within 1 hours.