def mean(ls):
''' numerical iterator --> float
calculate the mean value of the list elements.
>>>mean([10, 15, 20])
15
>>>mean(range(1,4))
2.0
'''
avg=0.0
sum=0
count=0
for i in ls:
sum=sum+i
count+=1
avg = sum/count
return avg
def median(ls):
'''list of numbers --> number
return the median of the numbers list ls.
>>>median([20, 10 ,20, 30, 12, 17])
18.5'''
pass
ls.sort()
d = len(ls)
if d%2==0:
return ((ls[int(d/2)]+ls[int(d/2)-1])/2)
else:
return (ls[int(d/2)])
from math import pow
def geometric_mean(ls):
'''list of nature numbers --> number
return the geometric mean of the numbers list ls.
>>>geometric_mean([3, 6 ,4, 7, 1, 3])
3.387'''
pass
sum = 1
d = len(ls)
for i in ls:
sum *= i
return pow(sum,1/d)
def percentile(ls,n):
'''list of numbers, number from the list --> percent
return the percent of the numbers in the list that smaller than the number
>>>percentile([1,3,3,4,6,7], 4)
66.66666666666667%'''
ls.sort()
count=1
for x in ls:
if x==n:
p=count*100/len(ls)
else:
count+=1
return p
def frequency_tabels(ls):
'''list of numbers --> dictionary{key:value}
return the frequency of any number in the list
>>>frequency_tabels([1,3,3,4,6,7])
{1:1,3:2,4:1,6:1,7:1}'''
dic={}
for k in ls:
v=ls.count(k)
dic [k]=v
return dic
def data_range(ls):
'''list of numbers --> number
return the range of the values in the list
>>>data_range([1,3,3,4,6,9])
8'''
Max=max(ls)
Min=min(ls)
r=Max-Min
return r
def variance(ls):
'''list of numbers --> number
return the variance of the values in the list
>>>variance([1,3,3,4,6,9])
6.55555'''
su = 0
su1 = 0
for i in ls:
su+=i
u = su/len(ls)
for d in ls:
su1+=(d-u)**2
return su1/(len(ls)-1)
def std (ls):
'''list of numbers --> number
return the variance of the values in the list
>>>data_range([1,3,3,4,6,9])
8'''
return variance(ls)**0.5