forked from minaevd/hackerrank-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitch1.py
More file actions
executable file
·58 lines (45 loc) · 1.67 KB
/
Copy pathtwitch1.py
File metadata and controls
executable file
·58 lines (45 loc) · 1.67 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
#!/usr/bin/python
import csv, sys, numpy
class Table:
def __init__(self, tbl):
self.tbl = tbl
def compute(self):
names = self.tbl.pop(0)
values = map(list, zip(*self.tbl))
i = 0
for nm in names:
values[i] = map(float, values[i])
print nm + \
": mean " + str(sum(values[i]) / len(values[i])) + \
", median " + str(numpy.median(values[i])) + \
", stdev " + str(numpy.std(values[i]))
i+=1
class TableLoader:
def __init__(self):
pass;
def load_file(self, filepath):
with open(filepath, 'rb') as f:
reader = csv.reader(f)
data = list(reader)
return Table(data)
# Your code must call this function
def run_test(csv_path):
loader = TableLoader() # implement this
# Load a CSV file
table = loader.load_file(csv_path)
# Compute and print the mean, median and stdev of each series
table.compute()
# Note:
# mean: This is the "average" of a list of numbers. Each value contributes
# equally to the final result.
# median: This is the "middle" value of a list of numbers; half of the numbers
# in the list are smaller than the median, and half are larger.
# stdev: This is a measure of how much the numbers in the list differ from the
# mean. For each value, take the square of the difference between the
# value and the mean. Take the mean of the squares, and then take the
# square root of that mean squared value.
if __name__ == '__main__':
if len(sys.argv) > 1:
run_test(sys.argv[1])
else:
print "Print usage ..."