-
Notifications
You must be signed in to change notification settings - Fork 0
/
running_median.py
32 lines (27 loc) · 924 Bytes
/
running_median.py
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
import os.path
import sys
def processInputFile(fileName):
if os.path.exists(fileName):
getInputList = [inputLine.rstrip('\n') for inputLine in open(fileName)]
processedIntResultList = list(map(int, getInputList))
processedIntResultList.sort()
new_list = []
for i in processedIntResultList:
new_list.append(i)
print ("{:.1f}".format(runningMedian(new_list)))
else:
raise Exception('{} provided does not exists'.format(fileName))
def runningMedian(runningList):
length = len(runningList)
isOdd = (length % 2) == 0
if isOdd:
index = int(length/2)
value = (runningList[index]+ runningList[index-1]) / 2
else:
if length == 1:
value = runningList[0]
else:
index = int((length + 1) /2)
value =runningList[index-1]
return value
processInputFile(sys.argv[1])