Median


#1



Here is my code and i know there are mistakes here - m = len(l)/2 and here - m = float((len(l)/2) / ((len(l)/2) - 1))
i know i should use something instead of len(l) to use numbers instead of lengh but i can't imagine how to do this


def median(l):
    sorted(l)
    m = 0
    if len(l) % 2 == 0:
        m = len(l)/2
    elif len(l) == 1:
        m = 1
    else:
        m = float((len(l)/2) / ((len(l)/2) - 1))
    return m


#2

i found number's indices with the m = len(l) / 2 but i don't know how to use this index


#3

lets start here:

sorted(l)

you sort the list, but you don't store the sorted list anywhere. well, len(l)/2 will give you the index, so then you can access items from the list by using the following syntax:

listName[indexName]

indexName can be a variable or a calculation.


#4

def median(l):
    lst = sorted(l)
    m = 0
    if len(lst) % 2 == 0:
        m = lst[len(lst) / 2]
    elif len(lst) == 1:
        m = 1
    else:
        m = lst[float((((len(lst) / 2) + ((len(lst) / 2) - 1) / 2)))]

    return m

I am sorry for this string that is difficult to read
there's error message

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


#5

if a line is difficult to read, maybe we can do something about it? Hold on.

this handles:

if len(lst) % 2 == 0:
        m = lst[len(lst) / 2]

list which have an even legnth (4, 6, 8 ) and this:

else:
        m = lst[float((((len(lst) / 2) + ((len(lst) / 2) - 1) / 2)))]

handles list with odd number of items? Does that make sense?


#6

Could you please show me how to use

with example?


#7

did you fix the handling for lists with odd/even number of items?


#8

def median(l):
    lst = sorted(l)
    m = 0
    if len(lst) % 2 == 0:
        m = lst[float((((len(lst) / 2) + ((len(lst) / 2) - 1) / 2)))]
    elif len(lst) == 1:
        m = 1
    else:
        m = lst[len(lst) / 2]

    return m

#9

get both the right middle and left middle, how do you think this can be done? You need to access lst for both left middle and right middle, maybe store this in variable first?


#10

def median(l):
    lst = sorted(l)
    m = 0
    if len(lst) % 2 == 0:
        m1 = lst[len(lst) / 2]
        m2 = 1st[len(lst) / 2) - 1)]
        m = float((m1 + m2) / 2)
    elif len(lst) == 1:
        m = 1
    else:
        m = lst[len(lst) / 2]

    return m

  File "python", line 6
    m2 = 1st[len(lst) / 2) - 1)]
           ^
SyntaxError: invalid syntax

#11

1st vs lst, this are different things


#12

Oh my god sorry it's so silly


#13

def median(l):
    lst = sorted(l)
    m = 0
    if len(lst) % 2 == 0:
        m1 = lst[len(lst) / 2]
        m2 = lst[(len(lst) / 2) - 1]
        m = float((m1 + m2) / 2)
    elif len(lst) == 1:
        m = 1
    else:
        m = lst[len(lst) / 2]

    return m

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


#14

so here:

m = float((m1 + m2) / 2)

first m1+m2 happens (which is 9), then 9 is divided by 2. Which gives four (dividing two integers gives an integer as result, if needs be the number is floored) then you cast this integer to a float. You don't want the number to be floored


#15

Thank you it's solved now!


#16