Skip to content

Instantly share code, notes, and snippets.

@dradecic
Created September 14, 2019 12:39
Show Gist options
  • Save dradecic/7f295913c01172ffebe84052c8158703 to your computer and use it in GitHub Desktop.
Save dradecic/7f295913c01172ffebe84052c8158703 to your computer and use it in GitHub Desktop.
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