Median


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


I know that there is a longer way to do this problem, however people can get around it but utilizing "numpy". I think there should be something saying not to use numpy... just a suggestion.


import numpy

def median(numbers):
    return numpy.median(numbers)


#2

you come here to learn, if you want to use numpy's build in numpy, then that is your choice. Why should we block you from this?


#3

def median(lst):
lst1=sorted(lst)
x=len(lst1)

if x%2==0:
    l1=(int)(x/2)
    l2=l1-1

    median=float(lst1[l1]+lst1[l2])/2


else:
    median=lst1[x/2]

return median

#4

def median(lst):
    
    lst = sorted(lst)  
    length = len(lst) - 1
    
    if length % 2 == True:
        return (lst[length / 2 ] + lst[(length / 2 ) + 1]) / 2.0
        
    if length % 2 == False:
        return lst[length / 2]

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.