# Sorted problem

#1

``````def median(x):
n = sorted(x)
is_median = 0
length = len(n)
if length % 2 == 0:
is_median = (n[length/2] + n[length/2 - 1]) / 2
else:
is_median = n[length/2]
return is_median``````

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

def median(x):
x.sorted
is_median = 0
length = len(x)
if length % 2 == 0:
is_median = (x[length/2] + x[length/2 - 1]) / 2
else:
is_median = x[length/2]
return is_median

it returns "median([1]) resulted in an error: 'list' object has no attribute 'sorted'"

i try to deal with number by index

#2

bug in that line..

In this line you get two numbers and divide by 2.

suppose mid numbers are : 3 and 2
you do this..
`(3 + 2) / 2` and you think that it will return `2.5`?
Actually it returns `2`.

Its nature of `/` in Python 2.0 version

In python 2.0

`6 / 5` results into `1`
It does not return float value..(decimal value)
to have float value as result..
you need to make one of the numbers float.

`6/5.0` will give you `1.2`

In python 3.0
Things are different.
`/` gives float value..
`6/5` gives `1.2`

#3

ohh right!! thank you so much, now i get it

#4

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