Median


#1


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

Though on my installed python console, 4.5 is correctly returned as median. I keep getting the error message as "Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5"


def median(input_list):
    input_list=sorted(input_list)
    n=len(input_list)
    m=(n/2)-0.5
    o=(n/2)
    p=(n/2)-1
    if n%2==1:
        median=input_list[int(m)]
    if n%2!=1:
        median= (input_list[int(p)]+input_list[int(o)])/2
    return median


#2

I don't really understand your code at all, I'm sure there's many ways to do this, but I'll try to give some insight by explaining my method and hopefully you can apply it to your own.

def median(taken_list):
    taken_list = sorted(taken_list) #sorts the list
    mid_point = (len(taken_list) / 2) #finds the mid point of the list
       if len(taken_list) % 2 == 0: #finds "if" there's an even length of list, and if so;
           return ((taken_list[mid_point - 1]) + (taken_list[mid_point])) / (2.0) #returns the equation. (see reasoning below)
       else:
           return taken_list[mid_point] # if not even length of list (else) return the the value of the taken list at the index [mid_point]

Reason for the "even equation" ;
1. ( (1 down from midde) + (1 up from middle) ) / 2
2. list start at index 0, and length counts from 1, you don't need to +1 the second "(taken_list[mid_point])"

Advice
1. To make you code easier for others to understand, I'd advice against using 1 letter variable, and instead use variable that have descriptive names.
2. You can easier debug you code by using print.
For example I took your code and then under it used:

print median([4, 5, 5, 4]) #prints 4, should print 4.5
print median([0, 1, 2, 3, 4]) #prints 1, should print 2

From this it looks like you're code failing on both odd and even length lists, they both look to be giving less.

Sorry I couldn't be anymore help, but I hope this gives you hints as you'll understand your code more.


#3

is the equivalent of,

input_list.sort()

If you wish to preserve the original list (which may be in global scope), then assign to a new local variable.

s = sorted(input_list)

Some of the trouble is related to how Python 2.x.x treats integer operations.

int1 / int2 => yields an integer

float1 / int2 => yields a float

Working with simple logic gives the following...

n = len(input_list)

m = n / 2

if n % 2:
    return s[m]

return float(s[m - 1] + s[m]) / 2

#4

for the average, divide by 2.0 instead of 2.
So median= (input_list[int(p)]+input_list[int(o)])/2 would be
median= (input_list[int(p)]+input_list[int(o)]) / 2.0

this makes it a float instead of an integer.


#5

here check my code maybe you might understand better though the code by @guy12 is quite clever :
:wink:

def median(lists):
lists = sorted(lists)
length = len(lists)
temp = length/2
if length%2 == 0:
avg = (lists[temp] + lists[temp-1]) /2.0
else:
avg = lists[temp]
print avg
return avg

Here initially i have sorted and taken the length of list, then divided the length by 2 and stored it in temp. Now if length is even take average of middle two elements as median else if length is odd take the middle element as the median. Lastly print and return the median


#6

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