Median


#1

i dont know the code working good outside the codecademy
but here not good i cant define the problem exactly

def median (lst):
    x=sorted(lst)
    avg = int((len(x))/2 -.5)
    print(avg)
    print(x[avg])
    print(sorted(lst))
    if avg %2 == 0 :
        return x[avg]
    else :
        print("here")
        y = float(x[avg]+x[avg+1])/2
        return y

print(median([6, 8, 12, 2, 23]))

median([6, 8, 12, 2, 23]) returned 7.0 instead of 8


#2

i think its important to understand that codecademy runs python2 where as you seem to run python3, a big difference is division behavior:

# python2
print 5 / 2 # 2
# python3
print 5 / 2 # 2.5

this is important here:

avg = int((len(x))/2 -.5)

lets see the length is 5, then we get:

# python3 
print(int(5/2-.5) # 2
# python2
print int(5/2-.5) # 1

personally, if you want it to work for both versions of python use floor division:

# python3
print(5//2) # 2
# python2
print 5//2 # 2

i think its the best way to go


#3

when i write avg = len(x)//2 or /2 same result = 2 in python 2
but i need to get output float like 2.5 to go through the if condition …
but the return always 2 even i did write avg = float(len(x)//2)

i need if it 5 items so it mean 2.5 so not in if avg %2 ==0 and will go to else .

thank you in advance


    print(len(x)) #5
    print(len(x)//2 )#2
    print(float(len(x))//2) #2.0
    print(int(len(x)//2)) # 2

#4

you shouldn’t use avg for if condition, you should use the length for the if condition.


#5

i used avg dirct inside if but also same problem …i need two different results ::
1:if i have 6 elements ---- so go the second condition— return y = (x[len(x)//2]+x[(len(x)//2)+1])//2.0
that mean [4,5,5,4] —after sorting[4,4,5,5]— 4+5 = 9 // 2 = 4.5 —[4/2 +]

2:if we have 5 elements —so go to the first condition ----return x[(len(x)//2)-0.5]
[2,6,8,23,12]–after sorting

def median (lst):
    x=sorted(lst)
    # print(len(x)) #5
    # print(len(x)//2 )#2
    # print(float(len(x))//2) #2.0
    # print(int(len(x)//2)) # 2
    avg =(len(x)//2)
    # print(avg)
    # print(x[avg])
    # print(sorted(lst))
    if (len(x)//2) %2 == 0 :
        return x[(len(x)//2)]
    else :
        print("here")
        y = (x[avg-1]+x[avg])//2.0
        return y

print(median([4,5,3,6]))

#6

for which we can use the length, for even lists the length divisible by 2 has a remainder of zero, for odd lists the length divided by 2 has a remainder of 1


#7

finallyyyyyyyyyyyyyyyyyyy :slight_smile:
it’s okkkkkkkkkkkkk

from __future__ import division
def median (lst):
    x=sorted(lst)
     avg =(len(x)/2)-0.5
    print(avg)
    if (avg) %2 == 0 :
        return x[int(avg)]
    else :
        y = ((x[int(avg-0.5)]) + (x[int(avg+0.5)]))/2
        return y

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

#8

i think we must from future import division

to get the result float by the division


#11

you are really complicating matters for yourself by doing -.5 and stuff, and trying to solve this problem in both python2 and python3 at the same time

you should use the length to determine if the list is of even or uneven length, not avg

then you can really simplify your code, in particular if you look into how floor division operator (//)


#12

i think you are totally right … so the next code ok ? and simple ?
i think i lost alot of time to think with the difficult way but may be all of this experience or bad thinking hahahah i don’t know


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

    if (len(x)) %2 == 1 :
        y = len(x)//2
        return x[int(y)]
    else :
        y = len(x)//2
        y = ((x[int(y-1)]) + (x[int(y+1)]))/2
        return y

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

#13

yep, that looks a lot better :slight_smile: well done :smiley:


#14

really thanks for your great effort


#15

no problem, any questions left?


#16

no you can close it :grin::grin::rofl:


#17