median>>>TAKE A LOOK & HELP


#1

This is my code below, I don't know what's wrong with it, please help.

def median(old_list):
new_list = []
old_list.sort()
old_list.append(new_list)
return new_list


#2

The problem asked to sort the old_list and return the middle value (in case of odd numbered list) or average of the middle values (in case of even numbered list).

Lets see what your function does. First it creates an empty list called new_list. Then it sorts the old_list (which is required by the problem). After that it appends the empty list to your old_list & returns that value, which is nowhere near what was asked by the problem.

Your program is alright up to this much:

Now, do the following:

  1. Declare a variable that can store the length of the list. In case you forgot, length of a list can be determined using the len() function.

  2. Now add an if statement to check if the old_list is even numbered. e.g.if length % 2 == 0

Say you have a list old_list = [1, 4, 2, 4, 0, 5] which is even numbered. You have to return the average of 2 and 4 i.e. 3. The following instructions should be under the if statement:

  1. You need to access the indexes 2 and 3 respectively. If you divide the length of your list by 2, you get 3. So,old_list[length/2] gives 4.

  2. Same way, try to get the value under the index 2.

  3. Take the average of the two values and return that value. You may come up with this:

Code
  `return (old_list[length/2] + old_list[(length/2)-1]) / 2.0`

The following instruction goes into else statement:

  1. For odd numbered list, just return the middle indexed element. i.e. return old_list[length/2]. Say, number of elements in old_list = [1, 2, 1] i.e. 3. You need to return the middle element 2. old_list[length/2] = old_list[3/2] = old_list[1] (1.5 is rounded up to 1 because both numbers are integer).

#3

I got stuck at trying to access the indexes 2 and 3.


#4

I have already given the code to access the middle two indexes for even numbered list.

return (old_list[length/2] + old_list[(length/2)-1]) / 2.0

The code that I used to solve this problem is given below. See if you can make sense out of it.

def median(tuna):   # here tuna is similar to old_list
    tuna.sort()
    if len(tuna) % 2 == 0: # you can replace len(tuna) by length = len(tuna)
        return (tuna[len(tuna)/2] + tuna[(len(tuna)/2)-1])/2.0
    else:
        return tuna[len(tuna)/2]

#5

For this problem, anything that looks complicated is too complex. We can solve this with sheer simplicity.

What do we need?

  1. a sorted sample space
  2. number of terms
  3. middle term, or average of middle two terms.

def median(sample):
    sample.sort()
    n = len(sample)
    m = n // 2
    if n % 2:
        return sample[m]                        # edited
    return float(sample[m - 1] + sample[m]) / 2

It's basically the same as above, only simplified, just as we would do in Maths, and it is D.R.Y.


#6

... typo, should be ...

return sample[m]

EDIT: typo has been fixed.


#7

My dyslexia... Thank you for pointing that out. Fixed now.


#8

With your kind help I was able to understand the code. Thank you very much!


#9

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