Update median.md

This commit is contained in:
Prajwal
2019-10-03 16:38:21 +05:30
committed by GitHub
parent ebadaa8c37
commit b974426bcd

View File

@ -1,6 +1,6 @@
--- ---
title: median title: median
tags: math,median,beginner tags: math,beginner
--- ---
Finds the median of a list of numbers. Finds the median of a list of numbers.
@ -11,12 +11,10 @@ def median(list):
list.sort() # The sort function of python list.sort() # The sort function of python
list_length = len(list) list_length = len(list)
if list_length%2==0: if list_length%2==0:
return (list[int(list_length/2)-1] + list[int(list_length/2)])/2 # Mean of the middle two elements return (list[int(list_length/2)-1] + list[int(list_length/2)])/2
else: else:
return list[int(list_length/2)] # The element in the middle return list[int(list_length/2)]
``` ```
```py ```py
median([1,2,3]) # 2 median([1,2,3]) # 2
median([1,2,3,4]) # 2.5 median([1,2,3,4]) # 2.5