Advertisement
SergeySterkhov

Untitled

Dec 16th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import sys
  6. from math import log10
  7. class Mapper:
  8.     def run(self):
  9.         data = self.readInput()
  10.         for cur_id, follow1 in data:
  11.             follow = int(follow1)
  12.             if follow == 1:
  13.                 group = 0
  14.             else:
  15.                 group = int(log10(follow-1))
  16.             print('%d\t1' % group)
  17.  
  18.     def readInput(self):
  19.         for line in sys.stdin:
  20.             yield line.encode("utf8").strip().split('\t', 1)
  21.  
  22. class Reducer:
  23.     def run(self):
  24.         sys.stderr.write('reporter:status:Reducer started\n')
  25.         data = self.readInput()
  26.         last_group = -1
  27.         last_count = 0
  28.         for group1, one in data:
  29.             group = int(group1)
  30.             if last_group == group:
  31.                 last_count += int(one)
  32.             else:
  33.                 if last_group != -1:
  34.                     print(self.makestring(last_group)+'\t'+str(last_count))
  35.                 last_group = group
  36.                 last_count = int(one)
  37.         print(self.makestring(last_group)+'\t'+str(last_count))
  38.  
  39.     def readInput(self):
  40.         for line in sys.stdin:
  41.             yield unicode(line, 'utf8').strip().split('\t', 1)
  42.  
  43.     def makestring(self, k):
  44.         if k == 0:
  45.             s = "["
  46.         else:
  47.             s = "("
  48.         s = s + str(10**k)+", " + str(10**(k+1))+"]"
  49.         return s
  50.  
  51. class Combiner:
  52.     def run(self):
  53.         sys.stderr.write('reporter:status:Reducer started\n')
  54.         data = self.readInput()
  55.         last_group = -1
  56.         last_count = 0
  57.         for group1, one in data:
  58.             group = int(group1)
  59.             if last_group == group:
  60.                 last_count += int(one)
  61.             else:
  62.                 if last_group != -1:
  63.                     print(str(last_group)+'\t'+str(last_count))
  64.                 last_group = group
  65.                 last_count = int(one)
  66.         print(str(last_group)+'\t'+str(last_count))
  67.  
  68.     def readInput(self):
  69.         for line in sys.stdin:
  70.             yield unicode(line, 'utf8').strip().split('\t', 1)
  71.  
  72. if __name__ == "__main__":
  73.     my_func = sys.argv[1]
  74.     if my_func == "map":
  75.         mapper = Mapper()
  76.         mapper.run()
  77.     elif my_func == "reduce":
  78.         reducer = Reducer()
  79.         reducer.run()
  80.     elif my_func == "combine":
  81.         combiner = Combiner()
  82.         combiner.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement