10 Days of Statistics my solution

Environment: Python2

Day 0: Mean, Median, and Mode

# Enter your code here. Read input from STDIN. Print output to STDOUT
wtf=raw_input()
wtf2=raw_input()

print wtf,type(wtf)
print wtf2, type(wtf2)

# Your Output (stdout)

10 <type 'str'>
64630 11735 14216 99233 14470 4978 73429 38120 51135 67060 <type 'str'>

my solution 

def quartile_1(l):
    return sorted(l)[int(len(l) * .25)]

def median(l):
    return sorted(l)[len(l)/2]

def quartile_3(l):
    return sorted(l)[int(len(l) * .75)]

 

li=wtf2.split()
ll=[float(i) for i in li]


def mean(x):
    return sum(x)/len(x)

def median(x):
    x.sort()
    while len(x)>2:
        x=x[1:-1]
    return sum(x)/len(x)
    
def mode(x):
 x=[int(i) for i in li]
 dic={}
 for i in x:
 dic[i]=0
 for i in x:
 dic[i]+=1
 m=max(dic.items(), key=lambda x: x[1])[1]
 
 c=[]
 for i in dic.items():
 if i[1]==m:
 c.append(i[0])
 return min(c)
 
print mean(ll)
print median(ll)
print mode(ll)


Environment: R

# Enter your code here. Read input from STDIN. Print output to STDOUT

x <- suppressWarnings(readLines(file("stdin")))
x <- strsplit(x,' ')
x <- lapply(x,as.numeric)[[2]]
#print(x)
print(mean(x))
print(median(x))

getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}
print(min(x))

Day 0: Weighted Mean

# Enter your code here. Read input from STDIN. Print output to STDOUT
c=raw_input()
a=raw_input()
b=raw_input()

# Input (stdin)
# 5
# 10 40 30 50 20
# 1 2 3 4 5


a=[float(i) for i in a.split(' ')]
b=[float(i) for i in b.split(' ')]

up = [i*j for i,j in zip(a,b)]

print round(sum(up)/sum(b),1)

Day 1: Quartiles

Sample Input

9
3 7 8 5 12 14 21 13 18

Sample Output

6
12
16

Explanation

Lower half (L): 3, 5, 7, 8

Upper half (U): 13, 14, 18, 21

def quartile_1(l):
    return sorted(l)[int(len(l) * .25)]

def median(l):
    return sorted(l)[len(l)/2]

def quartile_3(l):
    return sorted(l)[int(len(l) * .75)]

 

# Enter your code here. Read input from STDIN. Print output to STDOUT

y=raw_input()
x=raw_input()

y=int(y)
x=[int(i) for i in x.split(' ')]
x.sort()

if y%2==0:
    print median(x[:x.index(median(x)[1])+1])[0]
else:
    
    print median(x[:x.index(median(x)[1])])[0]
 
print(median(x)[0])
    
if y%2==0:
     print median(x[x.index(median(x)[2]):])[0]
else:
     print median(x[x.index(median(x)[2])+1:])[0]

if y%2==0:
    print median(x[:x.index(median(x)[1])+1])[0]
else:
    
    print median(x[:x.index(median(x)[1])])[0]
 
print(median(x)[0])
    
if y%2==0:
     print median(x[x.index(median(x)[2]):])[0]
else:
     print median(x[x.index(median(x)[2])+1:])[0]


Or

# Enter your code here. Read input from STDIN. Print output to STDOUT


y=raw_input()
x=raw_input()

y=float(y)
x=[int(i) for i in x.split(' ')]
x.sort()

if y%2==1:
    m=round(y/2)
    l=(m-1)/2
    r=m+l
    
    m,l,r=int(m),int(l),int(r)
    
    print (x[l-1]+x[l])/2
    print x[m-1]
    print (x[r-1]+x[r])/2
    
if y%2==0:
    m=y/2
    l=round(m/2)
    r=m+l
    
    m,l,r=int(m),int(l),int(r)

    if m%2==0:
        print (x[l-1]+x[l])/2
        print (x[m-1]+x[m])/2
        print (x[r-1]+x[r])/2
    else:
        print x[l-1]
        print (x[m-1]+x[m])/2
        print x[r-1]        

Day 1: Standard Deviation

Sample Input

5
10 40 30 50 20

Sample Output

14.1
# Enter your code here. Read input from STDIN. Print output to STDOUT
y=raw_input()
y=int(y)
x=raw_input()


x=[int(i) for i in x.split(' ')]
x.sort()

m=sum(x)/float(len(x))
square=map(lambda x:(x-m)**2, x)
mu=(sum(square)/len(x))**0.5

print mu

 

Leave a Reply

Your email address will not be published.

Notice: Undefined index: cookies in /var/www/html/wp-content/plugins/live-composer-page-builder/modules/tp-comments-form/module.php on line 1638