Skip to content

Instantly share code, notes, and snippets.

@morisasy
Created December 30, 2017 11:12
Show Gist options
  • Save morisasy/80dffe01bdbaac7de709524ecfa2d52c to your computer and use it in GitHub Desktop.
Save morisasy/80dffe01bdbaac7de709524ecfa2d52c to your computer and use it in GitHub Desktop.
def median(numbers):
numbers.sort()
if len(numbers) % 2:
# if the list has an odd number of elements,
# the median is the middle element
middle_index = int(len(numbers)/2)
return numbers[middle_index]
else:
# if the list has an even number of elements,
# the median is the average of the middle two elements
right_of_middle = len(numbers)//2
left_of_middle = right_of_middle - 1
return (numbers[right_of_middle] + numbers[left_of_middle])/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment