# 15/15 median

#1

"The program took too long to finish. Check your code for infinite loops and try again."

``````def median(x):
a=[]
b=0
c=0
while len(a)!=len(x):
for l in x:
if l<b and l not in a:
a.append(l)
else:
a=a
b+=1
if len(a)%2!=0:
while len(a)!=1:
del a[0]
del a[len(a)-1]
for z in a:
c+=z
else:
while len(a)!=2:
del a[0]
del a[len(a)-1]
for z in a:
c+=z
c=c/2.0
return c``````

#2

The above looks to be an attempt at a sort algorithm. Problem is that len(a) is always nont equal to len(x), hence the infinite loop.

#3

You are correct that it is a sorting algorithm. I am attempting to sort the items in list x from smallest to largest. Can You explain why len(x) and len(a) are always not equal? The odd thing is that I tested that section individually when I first wrote it by returning a and printing the function with several different inputs. It seemed to work fine.

#4

This is preventing duplicates. A statistical sample is considered to be a list of random values, with duplication permitted. We do not remove or exclude duplicates. Doing that guarantees that `a` will always be shorter than `l`.

#5

Ok. Now I see where I went wrong. Thanks.

#6

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