Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
art7_median
inputs_odd = [3, 1, 2, 5, 4]
inputs_even = [3, 1, 2, 5, 4, 6]
def get_median(lst):
lst = sorted(lst)
lst_len = len(lst)
index = (lst_len - 1) // 2
if lst_len % 2:
return lst[index]
else:
return (lst[index] + lst[index + 1]) / 2.0
print(get_median(inputs_odd)) # 3
print(get_median(inputs_even)) # 3.5
print(np.median(inputs_odd)) # 3
print(np.median(inputs_even)) # 3.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.