Median


#1

Working fine in most cases , but unable to sort symmetric lists(read [4,5,5,4])
As far as I could debug,I guess the problem is in the sorting section.

def sort(lst):
l=len(lst)
for i in range(l):
min=lst[i]
pos=i
j=i+1
for j in range(1,l):
if lst[j]<=min:
min=lst[j]
pos=j
t=lst[i]
lst[i]=lst[pos]
lst[pos]=t
return lst
def median(lst):
l=len(lst)
lst=sort(lst)
if l%2==0:
med=lst[(l-2)/2]+lst[l/2]
return med/2.0
else:
return lst[l/2]


#2

your sorting algorithm isn't working, the list remains [4,5,5,4] while it should be sorted to [4,4,5,5]

why write your own sorting algorithm? Why not use the built in sorted() function or .sort() method


#3

I completely forgot about the in-built sorting function.Thanks!


#4

Writing a sorting algorithm could be a separate challenge.

I am not really feeling like going through your sorting algorithm now, its a time consuming thing to do, or do you really want to use your own sorting algorithm?


#5

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