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

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

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:

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.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:`

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`

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

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

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

The following instruction goes into `else statement:`

- 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).

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]
```

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

What do we need?

- a sorted sample space
- number of terms
- 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.

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