-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1rd2b.py
executable file
·38 lines (33 loc) · 1 KB
/
q1rd2b.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
32
33
34
35
36
37
38
#!/usr/bin/env python
import sys
def parseInput():
for line in sys.stdin:
if len(line)>0:
line = line.strip()
yield line
def reducer():
current_key = None
current_list = []
for line in parseInput():
neighbor, income = line.split('\t')
try:
income = float(income)
except Exception:
pass
if current_key == neighbor:
current_list.append(income)
else:
if current_key and current_list:
current_list.sort()
median = current_list[len(current_list)/2]
print '%s\t%s' % (current_key, median)
current_key = neighbor
current_list = []
current_list.append(income)
if current_key == neighbor:
if current_key and current_list:
current_list.sort()
median = current_list[len(current_list)/2]
print '%s\t%s' % (current_key, median)
if __name__=='__main__':
reducer()