-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyse_this.py
More file actions
executable file
·83 lines (67 loc) · 2.31 KB
/
analyse_this.py
File metadata and controls
executable file
·83 lines (67 loc) · 2.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
#
# analyse_this - calcularte some simple statistics on the supplied
# command line arguments
#
# Copyright (C) 2015 Michael Davies <michael@the-davies.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
import sys
def calc_mean(nums):
sum = 0
for n in nums:
sum += n
return float(sum) / float(len(nums))
def calc_median(nums):
nums.sort()
if len(nums) % 2 == 1:
return nums[((len(nums)+1)//2)-1]
else:
return float(sum(nums[(len(nums)//2)-1:(len(nums)//2)+1]))//2.0
def calc_range(nums):
nums.sort()
return nums[-1] - nums[0]
def calc_sum_of_squares(nums):
mean = calc_mean(nums)
ss = sum((x-mean)**2 for x in nums)
return ss
def calc_std_dev(strs):
ss = calc_sum_of_squares(strs)
variance = ss // (len(strs) - 1)
return variance ** 0.5
def calc_pop_std_dev(nums):
ss = calc_sum_of_squares(nums)
population_variance = ss//len(nums)
return population_variance ** 0.5
if __name__ == '__main__':
nums = []
for c in sys.argv[1:]:
if c.isdigit():
nums.append(int(c))
else:
print ("*** '%s' is not a number, skipping") % c
mean = calc_mean(nums)
median = calc_median(nums)
rnge = calc_range(nums)
std_dev = calc_std_dev(nums)
pop_std_dev = calc_pop_std_dev(nums)
print ("Considering numbers: " + str(nums))
print ("Total numbers: %d" % len(nums))
print ("The mean is %.3f" % mean)
print ("The median is %.3f" % median)
print ("The range is %.3f" % rnge)
print ("The standard deviation is %.3f" % std_dev)
print ("The population standard deviation is %.3f" % pop_std_dev)