Media in 15


#1

My mess wont work :frowning: any suggestions?

def median(content):
import math
x=sorted(content)
n=0

med=0
for i in x:
    n+=1
if n==1:
    for i in x:
        med=x[n-1]
elif n%2.0==0:
    for i in x:
        med=(x[(n/2.0)-1]+x[n/2.0])/2.0

    
else:
    for i in x:
        med=x[math.ceil(n/2.0)-1]

return med

#2

This is essentially a procedural approach in place of the len() method. One might prefer the latter:

n = len(content)

Now dividing this by 2 will net an integer which if the length is odd, will be the median offset. If the length is even then this is the higher element of the median pair. The lower element will be offset by minus one.

m = n /2
if n % 2:
    med = x[m] / 2.0
else:
    med = (x[m] + x[m-1]) / 2.0

This should give you what you need, or at least form a basis for a simpler solution.


#3

that loop instead of the len command....
anyway, thanks for your reply good sir


#4

Aside:

Since the parameter is expected to be a list, we can illustrate this in our code by using the list.sort() method. It makes it more apparent what to expect as content (for a reader of the code).

x = content.sort()

Just a thought.


#5

def median(content):
x=sorted(content)
n=len(x)
m=n/2
if n%2:
med=(x[m]+x[m-1])/2.0
else:
med=x[m-0.5]
return med

seriously i dunno whats wrong....


#6

Should be,

med = x[m] / 2.0

#7

Fixed my typo. Sorry about that.


#9
def median(data):
    data.sort()
    if len(data) % 2 == 0:
        return (data[len(data) / 2-1] + data[len(data) / 2]) / 2.0
    return data[int(len(data) / 2 + 0.5)]

worked for me


#10

I can't figure where I'm going wrong. Calling median([4,5,5,4]) gives 4 instead of 4.5. Can you help?

def median(lst):

> #sort the list
m = sorted(lst)
x = len(m)
#if the length of lst is even, the median is the average of the 2 middle items in sorted lst
if x % 2:
#find the 1st middle item
    a = m[x / 2]
#find the 2nd middle item
    b = m[(x / 2) - 1]
#return the average
    return (a + b) / 2    
#if the length of lst is odd, the median is the middle item in sorted lst
else:
    c = m[(x - 1) / 2]
    return c

print median([4,5,5,4])


#11

To return a float, divide by 2.0.


#12

yes..it's work for me too..thanks..


#13

from math import floor
def sort(num_list):
result = []
for key in num_list:
for hey in num_list:
if key > hey :
result.append(hey)
else:
result.append(key)
return result
print result

def median (num_list):
new_list = sort(num_list)
dew = len(new_list)
if dew % 2 == 0:
return (( 2 * new_list[dew] ) + 1) / 2.0
else :
return floor (new_list[dew] / 2.0)

this is my code it shows error "list index out of range"


#14

Not sure if you tested your sort methd. When I passed it this list,

[54, 26, 93, 17, 77, 31, 44, 55, 20]

the result was,

[54, 26, 54, 17, 54, 31, 44, 54, 20, 26, 26, 26, 17, 26, 26, 26, 26, 20, 54, 26, 93, 17, 77, 31, 44, 55, 20, 17, 17, 17, 17, 17, 17, 17, 17, 17, 54, 26, 77, 17, 77, 31, 44, 55, 20, 31, 26, 31, 17, 31, 31, 31, 31, 20, 44, 26, 44, 17, 44, 31, 44, 44, 20, 54, 26, 55, 17, 55, 31, 44, 55, 20, 20, 20, 20, 17, 20, 20, 20, 20, 20]

which is not what we would expect. Python offers its own very powerful list sort method, so we don't really need to write our own, but it shows interest and initiative on your part.

Here is a not very efficient sort method called 'bubble sort' that is pretty fast on short lists, but abysmally slow on long ones:

num_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]

def bsort(lst):
    flag = True
    k = len(lst)-1
    while k > 0 and flag:
        flag = False
        for i in range(k):
            if lst[i] > lst[i+1]:
                flag = True
                lst[i],lst[i+1] = lst[i+1],lst[i]
        k = k - 1

bsort(num_list)
print num_list

Result:

[17, 20, 26, 31, 44, 54, 55, 77, 93]

Because lists are reference objects, we do not need to return anything from the function since num_list is sorted in place. lst is only a local reference to the global object.

Try this method in your program and we can look at the median function next.


#15

def median(content):
x=sorted(content)
n=len(x)
m=n/2
if n%2==0:
med=(x[m]+x[m-1])/2.0
else:
med=x[m-(1/2)]
return med

finally it worked, 0.5 should be 1/2 and n%2==0


#16

@textsolver87110. try diving by 2.0 so you will get double data type for your calculation


#17

Divide by a float to return a float. A 'double' is actually an integer, just double precision, meaning twice as many bits as single precision.


#18

that's the problem!

it should be if x % 2==0