-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
51 lines (47 loc) · 1.13 KB
/
stats.py
File metadata and controls
51 lines (47 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
from math import sqrt
import fileinput
# Write a program that computes typical stats
# Count, Min, Max, Mean, Std. Dev, Median
# No, you cannot import any other modules!
datainput = []
count = 0
for line in fileinput.input():
if line.startswith('#'): continue
line = line.rstrip()
datainput.append(float(line))
count += 1
datainput.sort()
max = datainput[-1]
min = datainput [0]
sum = sum(datainput)
mean = sum/count
tsqrdistance = 0
for numbers in datainput:
sqrdiastance = (numbers-mean)**2
tsqrdistance += sqrdiastance
std = sqrt(tsqrdistance/count)
center = (count+1) / 2
if center.is_integer() is True:
center = count // 2
median = datainput [center]
else:
gcenter = int(round(center))
lcenter = int(gcenter - 1)
median = (datainput[gcenter-1] + datainput[lcenter-1])/2
print(f'count: {count}')
print(f'Minimum: {min}')
print(f'Maximum: {max}')
print(f'Mean: {mean}')
print(f'Std. dev: {std:.3f}')
print(f'Median: {median}')
#OSFrog
"""
python3 stats.py numbers.txt
Count: 10
Minimum: -1.0
Maximum: 256.0
Mean: 29.147789999999997
Std. dev: 75.777
Median 2.35914
"""