Skip to content

Instantly share code, notes, and snippets.

@dradecic
Created September 14, 2019 12:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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