Median


#1
def median(x):
  b=sorted(x)
  print x
  print b
  a = len(b)
  for i in b:
      if a==1 :
        return b[0]
      elif (a%2)!=0:
        return b[(a+1)/2]
      elif a%2==0:
        return (b[a/2] +b[(a/2)+1])//2

the error message says "it returns 5 instead of 4.5 in case of [4,5,5,4]
please help


#2

why do you have a for loop? You created a loop, but you don’t do anything with it

also, here:

return (b[a/2] +b[(a/2)+1])//2

why +1?

Also, using floor division (//) you will certainly not get a float value returned


#3

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