Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #!/usr/bin/env python2.7
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import sys
  6.  
  7.  
  8. class FirstMapper:
  9. """
  10.  
  11. """
  12.  
  13. def __init__(self):
  14. """
  15.  
  16. """
  17. self.user_table = {}
  18.  
  19. def run(self):
  20. """
  21.  
  22. @return: n/a
  23. @rtype: None
  24. """
  25.  
  26. for user_id, follower_id in self.read_input():
  27. self.user_table[user_id] = 0 if user_id not in self.user_table.keys() else self.user_table[user_id] + 1
  28.  
  29. for user_id, follower_counter in self.user_table.iteritems():
  30. print('%s-%s\t' % (user_id, follower_counter)).encode('utf-8')
  31.  
  32. @staticmethod
  33. def read_input():
  34. """
  35. Generator function, that runs through standard input and
  36. yields line transformed into list of two integers.
  37.  
  38. @return: Two numbers, that in our case represent:
  39. - user_id
  40. - follower_id
  41. @rtype: list(int, int)
  42. """
  43.  
  44. for line in sys.stdin:
  45. yield list(map(int, unicode(line, 'utf-8').strip().split('\t', 1)))
  46.  
  47.  
  48. class Combiner:
  49. """
  50.  
  51. """
  52. pass
  53.  
  54.  
  55. class FirstReducer:
  56. """
  57.  
  58. """
  59.  
  60. def __init__(self):
  61. """
  62.  
  63. """
  64. pass
  65.  
  66. def run(self):
  67. """
  68. The main function in this class, that do all the work.
  69.  
  70. @return: n\a
  71. @rtype: None
  72. """
  73.  
  74. prev_user = []
  75. for user_id, follower_counter in self.read_input():
  76. if not prev_user:
  77. prev_user.append(user_id)
  78. prev_user.append(follower_counter)
  79. elif user_id == prev_user[0]:
  80. prev_user[1] += follower_counter
  81. else:
  82. print('%d-%d' % (prev_user[0], prev_user[1])).encode('utf-8')
  83. prev_user[0] = user_id
  84. prev_user[1] = follower_counter
  85.  
  86. @staticmethod
  87. def read_input():
  88. """
  89. Just generator func that yield each line from standard input, encoded.
  90.  
  91. @return:
  92. @rtype: unicode
  93. """
  94. for line in sys.stdin:
  95. yield list(map(int, unicode(line, 'utf-8').strip().split('-', 1)))
  96.  
  97.  
  98. class AnswerMapperFollowerCount:
  99. """
  100.  
  101. """
  102.  
  103. def __init__(self):
  104. """
  105. Default constructor for this class.
  106. Vars declared here:
  107. - follower_count - The number of users in the following ranges according to the number of subscribers:
  108. Like this - [1,10], (10, 100] (100, 10000], etc.
  109. - follower_avg - The average number of user subscribers.
  110. """
  111.  
  112. self.follower_count = {}
  113. self.follower_avg = None
  114.  
  115. def run(self):
  116. """
  117. The main function in this class, that do all the work.
  118.  
  119. @return: n\a
  120. @rtype: None
  121. """
  122.  
  123. data = self.read_input()
  124. number_of_users = 0
  125.  
  126. for user_id, follower_count in data:
  127. self.follower_count.setdefault("%s - %s" % (10 ** len(str(follower_count)) - 1,
  128. 10 ** len(str(follower_count))), []).append(1)
  129. if __debug__:
  130. print 11
  131. # sys.stderr.write("reporter:status:Processed %d documents\n" % doc_count)
  132. # sys.stderr.write("reporter:counter:MyCounters,InputWords,%d\n" % word_count)
  133. self.follower_avg += follower_count
  134. number_of_users += 1
  135.  
  136. self.emit_results()
  137.  
  138. @staticmethod
  139. def read_input():
  140. """
  141. Generator function, that runs through standard input and
  142. yields line transformed into list of two integers.
  143.  
  144. @return: Two numbers, that in our case represent:
  145. - user_id
  146. - follower_count
  147. @rtype: list(int, int)
  148. """
  149.  
  150. for line in sys.stdin:
  151. yield list(map(int, unicode(line, 'utf8').strip().split('\t', 1)))
  152.  
  153. def emit_results(self):
  154. """
  155. Pure function that emit the results that we got to reducer
  156. and then zero-outs all variables.
  157.  
  158. @return: n\a
  159. @rtype: None
  160. """
  161.  
  162. for _format, counts in self.follower_count.items():
  163. print('%s: %d' % (_format, counts)).encode('utf-8')
  164.  
  165. self.follower_count = {}
  166. self.follower_avg = None
  167.  
  168.  
  169. class AnswerMapperAvgFinder:
  170. """
  171.  
  172. """
  173.  
  174. def __init__(self):
  175. """
  176. Default constructor for this class.
  177. Vars declared here:
  178. - follower_level - Sum of all followers on this mapper.
  179. - number_of_users - Number of users per mapper.
  180. """
  181.  
  182. self.follower_level = 0
  183. self.number_of_users = 0
  184.  
  185. def run(self):
  186. """
  187. The main function in this class, that do all the work.
  188.  
  189. @return: n\a
  190. @rtype: None
  191. """
  192.  
  193. for user_id, follower_count in self.read_input():
  194. self.follower_level += follower_count
  195. self.number_of_users += 1
  196.  
  197. self.emit_results()
  198.  
  199. @staticmethod
  200. def read_input():
  201. """
  202. Generator function, that runs through standard input and
  203. yields line transformed into list of two integers.
  204.  
  205. @return: Two numbers, that in our case represent:
  206. - user_id
  207. - follower_count
  208. @rtype: list(int, int)
  209. """
  210.  
  211. for line in sys.stdin:
  212. yield list(map(int, unicode(line, 'utf8').strip().split('-', 1)))
  213.  
  214. def emit_results(self):
  215. """
  216. Pure function that emit the results that we got to reducer
  217. and then zero-outs all variables.
  218.  
  219. @return: n\a
  220. @rtype: None
  221. """
  222.  
  223. print('%s-%d' % (self.follower_level, self.number_of_users)).encode('utf-8')
  224.  
  225. self.follower_level = 0
  226. self.number_of_users = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement