Skip to content

Instantly share code, notes, and snippets.

@richardpascual
Last active June 18, 2021 23:09
Show Gist options
  • Save richardpascual/9c62aede067e52bc54e9f7e17ecca082 to your computer and use it in GitHub Desktop.
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.
#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]))
@richardpascual
Copy link
Author

This one doesn't work because the middle element of an odd count is not an integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment