Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, November 30, 2012

Codecademy: median


#sample solution


import math
aList = [4,4,5,5]
def median(aList):
#print aList
aList.sort()
#print aList
listLength = len(aList)
print str(listLength)
if listLength == 1:
#print aList[0]
return aList[0]
elif(listLength % 2 != 0):
index = (listLength/2)
#print index
return aList[index]
else:
index1 = (listLength/2)-1
#print index1
index2 = index1 + 1
#print index2
return (aList[index1]+aList[index2])/float(2)
print median(aList)

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts