Last active
June 18, 2021 23:09
-
-
Save richardpascual/9c62aede067e52bc54e9f7e17ecca082 to your computer and use it in GitHub Desktop.
Get the middle element. If it's an even number of elements, average the middle two values.
This file contains hidden or 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
#Write your function here | |
def middle_element(lst): | |
if len(lst) % 2 == 0: | |
sum = lst[int(len(lst)/2)] + lst[int(len(lst) / 2) - 1] | |
return sum / 2 | |
else: | |
return lst[int(len(lst)/2)] | |
#Uncomment the line below when your function is done | |
print(middle_element([5, 2, -10, -4, 4, 5])) | |
print(middle_element([5, 2, -4, 4, 5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This one doesn't work because the middle element of an odd count is not an integer.