Update median.md
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
---
|
||||
title: median
|
||||
tags: math,median,beginner
|
||||
tags: math,beginner
|
||||
---
|
||||
Finds the median of a list of numbers.
|
||||
|
||||
@ -11,12 +11,10 @@ def median(list):
|
||||
list.sort() # The sort function of python
|
||||
list_length = len(list)
|
||||
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:
|
||||
return list[int(list_length/2)] # The element in the middle
|
||||
|
||||
return list[int(list_length/2)]
|
||||
```
|
||||
|
||||
```py
|
||||
median([1,2,3]) # 2
|
||||
median([1,2,3,4]) # 2.5
|
||||
|
||||
Reference in New Issue
Block a user