Median


#1



I summon thy Stetim to aid my median problem!

I'm stuck at thinking of the code needed to solve this. In my head: If even then average the sorted middle two numbers, if odd then it's the sorted middle index.


def median(lits):
    lits.sort()


Replace this line with your code.


#2

Steps to achieve this will be..

1.Sort the list
2.Check if list have even or odd numbers of items in it.
3.If It has even numbers of items, we will take average of two mid items.
4.If it has odd number of items, then you have to just return the middle item.


#4

I can't think of the equations to average the two middle items in the even list or to just return the middle item in the odd.


#5

Lets start with odd lists, how could we calculate the middle index based on the length? take a few lists (list with length 3, 5 and 7 for example), think what are the indexes for lists with this length, which is the middle index? see if you can find a pattern.


#6

how do I return the middle number from the odd list?


#7

That is your task, for the following lists:

[1,2,3]
[1,2,3,4,5]
[1,2,3,4,5,6,7]

give me the: indexes, length and middle index


#8

If the list is odd or even, how do I write the following instruction to return those appropriate indexes.


#9

please do the task i give you, then we can talk about how we can calculate the middle index


#10

1st:
0 - 2, 2

2nd:
0-4, 3

3rd:
0-6, 4


#11

the list has a length of 3, and a middle index of 1. So i am not sure what the 2 is, 0-2 are the indexes, on that i agree :slight_smile:

see the pattern? the length divided by two gives us the middle index

same for the other lists, 2nd has the following indexes: 0, 1, 2, 3 and 4. Again the middle index is 2 and length is 5, same pattern (length / 2 gives middle index)


#12

oh right the middle INDEXwould be one, I confused the list data as the middle number. An Odd List would give us an even index and the opposite for an Even List, right?


#13

yes, we are working on indexes at the moment

The trick in this exercise is to get the middle indexes, once we have the indexes the rest is relatively easy

once you have the middle index:

middle_index = len([1,2,3]) / 2

getting the median (middle value) is easy:

lits[middle_index]

now, for even lists its a bit trickier.

lets say we have these lists:

[4,4,5,5]
[5,5,6,6,7,7]
[6,6,7,7,8,8,9,9]

how could we calculate the middle two indexes? Again, we have to use length (given we want it to work for list of any length)

Go do what we just did, looking at indexes and see what the middle two indexes are and how calculate them. Use the odd lists as inspiration


#14

I hope I'm slightly going in the right direction here.

def median(lits):
    lits.sort()
    x = lits
    if x % 2 == 1:
        middle_index = len(x) / 2
    return middle_index
        
    if x % 2 == 0:
        middle_index = (len(x)/2 - 1) + (len(x) / 2)/2.0
    return middle_index

#15

yea, that are some good steps in the right direction :slight_smile:

few problems left, here:

        middle_index = len(x) / 2
    return middle_index

return ends a function, so you want the return to have the same indent level as middle_index, otherwise you code doesn't reach the bit for even lists

now that we have the middle index, we still need to get the middle value:

middle_index = len(x) / 2
middle_value = x[middle_index]
# return the middle value, not the middle index
return middle_value

Now, for even lists its a bit trickier. Once we have the left and right middle index, we need to get the values from the list, add them together and divide them by 2.0


#16

Hey Stetim, ok so.

def median(lits):
    lits.sort()
    x = lits
    if x % 2 == 1:
        middle_index = len(x) / 2
        middle_value = x[middle_index]
        return middle_value
        
    if x % 2 == 0:
        middle_index = (len(x)/2 - 1) + (len(x)/2)/2.0
        middle_value = x[middle_index]
        return middle_value

How do we know where we are accessing the index and not the value, "len" brings the data from a list, right?


#17

if we want values from the list, we use square brackets, which we do here:

middle_value = x[middle_index]

otherwise we very likely have an index. I on purpose use variable names which also indicate what you are dealing with.

we have some issues here:

middle_index = (len(x)/2 - 1) + (len(x)/2)/2.0

this is not right, you will need two variables: middle_left_index (which should contain: len(x)/2 - 1 and middle_right_index (which should contain: len(x)/2)

now that we have the left and right middle index, we need to the right and left middle value, add them together and divide them by 2.0 to get the median. You added the indexes together, which is not right, you should add the values together


#18

def median(lits):
    lits.sort()
    x = lits
    if x % 2 == 1:
        middle_index = len(x) / 2
        middle_value = x[middle_index]
        return middle_value
        
    if x % 2 == 0:
        middle_left_index = len(x)/2 - 1
        middle_right_index = len(x)/2
        middle_value = middle_left_index + middle_right_index / 2.0
        return middle_value

#19

if you want to get from list index to list value you need to use square brackets, here is the general syntax:

listName[index]

here:

middle_value = middle_left_index + middle_right_index / 2.0

you are just adding indexes together, you will need to retrieve the values from list using this variable which contains the indexes

don't forgot math operations, division happens before adding unless you use parentheses


#20

def median(lits):
    lits.sort()
    x = lits
    if x % 2 == 1:
        middle_index = len(x) / 2
        middle_value = x[middle_index]
        return middle_value
        
    if x % 2 == 0:
        middle_left_index = len(x)/2 - 1
        middle_right_index = len(x)/2
        middle_value = [middle_left_index] + [middle_right_index] / 2.0
        return middle_value

Forgot to update it...


#21

before the square brackets you need to specify the list you want to retrieve the value from, look at odd lists how we achieved this:

x[middle_index]

see how i specify the list x of which i want to retrieve a value?

apply the same logic to your even lists