Median


#1



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


Oops, try again. median([4, 5, 5, 4]) returned 2.0 instead of 4.5


def median(x):
    y = sorted(x)
    for i in range(0, len(y)):
        if len(y) % 2 == 0:
            f = len(y)/2
            s = len(y) - f
            m = (f + s)/2.0
            return m
        elif len(y) == 1:
            return float(y[0])
        elif len(y) > 2:
            return len(y)/2


I feel like It should work, but I don't seem to be getting anywhere.


#2

f and s are indexes of the list, not values of the list

use f and s as indexes to get values from the list, add them together and divide by 2.0


#3

well i fixed that...i think. Now it's returning 5 instead of 4.5.

if len(y) % 2 == 0:
f = y[len(y)/2]
s = y[len(y) - f]
m = (f + s)/2.0


#4

but now f contains a value from the list, not an index. So, you can't use f to calculate s


#6

I have tried with this code . I am getting the correct output but it is displaying the output as error. Can anyone help me. I have tested many cases. But I am getting the same error.


#7

but the exercise will also call the function:

median([4,5,5,4])

but will not call your sorting function. So, you should call your sort function inside your median function


#8

I have called the sorting with in median function itself. I have got the answer also.But still it is showing error.
I have called the function. It is behind the warning notification. So you were not able to see. sorry.Uploading...

def median(z):
value = 0
m=len(z)/2
if len(z)%2 == 0:
n=m-1
value= (z[n] + z[m])/2.0
else :
value = z[m]
return value
def sorting(x):
index =0
for item in x:
index +=1
for i in range(index,len(x)):
if item>= x[i]:
dup = x[i]
x[i] =item
item=dup
x[index-1]=dup
return x

y=[4,5,5,4]
print median(sorting(y))


#9

Hi @stetim94 I have got the output. I have called the sorting function with in the median function . Now i got the correct output. Thanks. Earlier i have called it directly and assigned the value while calling the funtion. I t was the mistake. :slight_smile: :slight_smile: :slight_smile:


#10

Pardon me for butting in. I couldn't help but notice that there was quite an accumulation of code, all in the aim of solving a simple problem.

We are after the median, which is the value at the middle point in a sorted sample space (data). When there are an even number of data points, then the middle two are averaged out, and that becomes the median. In either case the median is a value that represents the middle data point.


#11

If the list contains even length then it is getting the value of average of Middle Values. I have represented by doing the average of (z[n]+z[m])/2 . if the length is odd I have directly given the middle value represented by z[m]. @mtf . Please notify me if I am mistaken anywhere. Thanks. Then output is also correct .


#12

I'm only astounded by the nearly twenty lines of code. Surely that can be refined.


#13

Just forward me if there is any other simple code. I have added extra lines for clear understanding. Even I feel it can be minimized few more steps. Thanks for the help.


#14

Given a sorted sample space, it has a length.

n = len(x)

With zero-indexing, half of the length will give the index of the lowest value in the top half. One less than that index will be the highest value of the lower half.


#15

Can you please forward me the full fledged code of what you are describing. It would be really helpful for me in the future. I feel you were explaining Quick sort method. Isn't it?


#16

def median(lst):
index = 0
lst=sorted(lst)
print lst
for lst_index in range(len(lst)):
print lst_index
if lst_index % 2 == 0:
index = lst_index/2
print index
print lst[index]
return lst[index]
else:
index = lst_index / 2
print index ,"Index"
print lst[index],"first index value"
print lst[index+1],"secod index value"
x= lst[index]
y=lst[index+1]
median_value = (x+y)/2.0
return median_value


#17

Nothing so sophisticated as that. The median problem can be solved with very little code.

The middle index (or indices, as apply) is the integer of length divided by 2.

n = len(lst)

m = int(n / 2)

Odd length,

int(9 / 2)  => 4

0 1 2 3 4 5 6 7 8
        ^

Even length

0 1 2 3 4 5 6 7
      ^ ^

#18

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