Median problem


#1



def median(mylist) :
    mylist.sort()
    length=len(mylist)
    if length%2==1 :
        ans=mylist[length/2]
    else :
        ans=(mylist[length/2-1]+mylist[length/2])/2
        
    return ans


#2

Gives 4 instead of 4.5 in the list - [4,5,5,4] why ?


#3

Don't worry guys , got it . Was using 2 instead of 2.0 so was getting integer instead of float


#4

could you please explain why in a list of 6 items:

[0,1,2,3,4,5]

whe the length of the array divied by two returns 3 rather than 2?

is ti because the length is 6 items, and the 0th item is not counted in the division of LENGTH / 2?

Thanks


#5

well, there are 6 items, so 6 / 2 = 3.

this has nothing to do with the indexes, this is simply the numbers of items in the list (6) divided by 2.


#6

But then would 3, in this case represent the index 3 or the number 3 in the list?

In the above solution to this problem, as_71 uses:

ans=(mylist[length/2-1]+mylist[length/2])/2

and if you apply that to my 6 long list, [0,1,2,3,4,5], when you divide length by two you get 3, but then when you count three along the list you end at 2. but if you take the answer to legnth divided by 2 as an index, you get the actual 3. to maybe clear up some confusion if the list was [1,23,64,75,87,99] would length/2 = 3 still, referencing 75?

This is why I think the value of length/2 is referring to the index value 3, otherwise you would have to discount the first index, which is 0.


#7

yes, true. the length is 6. so 6 / 2 = 3, then you use this number to access the list:

lst =  [1,23,64,75,87,99]
len_lst = len( [1,23,64,75,87,99])
print len_lst # will output 6
len_lst = len_lst / 2
print len_lst # will output 3
print lst[len_lst] # will output 75

so then we are talking about index 3.


#8

Thanks a lot! I'm trying to get my head around the terminology and gain a deeper understanding of how stuff works. which is hard when you jsut run through the exercises. but this definitely helped!


#9

yea, the whole point of the practice makes perfect exercise section is to get this deeper understanding, they don't contain a whole lot of instructions, you have to figure it out on your own.

If you like such challenges, i can highly recommend codewars, those are good challenges


#10


i have this problem where codecademy tells me the answer of my code is 4 and on idle(python platform) it tells me its 4.5 which is actually the right answer. I literally copy pasted it(you can see that its exactly the same on codecademy and python client) and i don't know whats wrong with it. all help are greatly appreciated thx
note: I've tried refreshing and submitting multiple times so...


#11

except your IDLE runs python3, and codecademy runs python2, and there is no backward compatibility.

one of the things of that was changed, is that in python3 dividing two integers will result in a float, where in python2 dividing two integer the result is floored resulting in a integer.

to overcome this problem, either divide by a float, or use float()


#12

thanks, i didnt know there was these changes thanks mate! everything make sense now xD


#13

i now only mentioned the thing currently relevant, but there are of course more significant changes between the two versions, the biggest one being print, in python2 print is being a keyword:

print "example"

where in python3 it is a function call, thus requiring parentheses:

print("example")

#14

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