Created
September 14, 2019 12:39
-
-
Save dradecic/7f295913c01172ffebe84052c8158703 to your computer and use it in GitHub Desktop.
art7_median
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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