Skip to content

Instantly share code, notes, and snippets.

@wizardxz
Created January 7, 2014 22:48
Show Gist options
  • Save wizardxz/8308379 to your computer and use it in GitHub Desktop.
Save wizardxz/8308379 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
def read_input(file):
for line in file:
yield float(line)
def main(separator='\t'):
data = read_input(sys.stdin)
for num in data:
print '%f%s%d' % (num, separator, 1)
if __name__ == "__main__":
main()
#!/usr/bin/env python
import sys
def read_mapper_output(file, separator='\t'):
for line in file:
value_text, multi_text = line.rstrip().split(separator, 1)
yield float(value_text), int(multi_text)
def read_by_two(iterable):
buf = None
for v in iterable:
if buf is None:
buf = v
else:
yield buf, v
buf = None
if buf is not None:
yield buf, None
def main(separator='\t'):
data = read_mapper_output(sys.stdin, separator=separator)
for (v1, m1), item2 in read_by_two(data):
if item2 is None:
avg, total_m = v1, m1
else:
v2, m2 = item2
avg = float(m1) / (m1 + m2) * v1 + float(m2) / (m1 + m2) * v2
total_m = m1 + m2
print "%f%s%d" % (avg, separator, total_m)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment