Advertisement
appertjt

REducer 5-3

Apr 20th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import sys
  2. import logging
  3.  
  4. from util import reducer_logfile
  5. logging.basicConfig(filename=reducer_logfile, format='%(message)s',
  6. level=logging.INFO, filemode='w')
  7.  
  8. def reducer():
  9. '''
  10. For every single unit, write a reducer that will return the busiest datetime (e.g.,
  11. the entry that had the most entries). Ties should go to datetimes that are later on in the
  12. Month of May. You can assume that the contents of the reducer will be sorted by UNIT, such that
  13. all entries corresponding to a given UNIT will be sorted together. The output should be the UNIT
  14. name, the datetime (which is a concatenation of date and time), and ridership separated by tabs.
  15.  
  16. For example, the output of the reducer may look like this:
  17. R001 2011-05-11 17:00:00 31213.0
  18. R002 2011-05-12 21:00:00 4295.0
  19. R003 2011-05-05 12:00:00 995.0
  20. R004 2011-05-12 12:00:00 2318.0
  21. R005 2011-05-10 12:00:00 2705.0
  22. R006 2011-05-25 12:00:00 2784.0
  23. R007 2011-05-10 12:00:00 1763.0
  24. R008 2011-05-12 12:00:00 1724.0
  25. R009 2011-05-05 12:00:00 1230.0
  26. R010 2011-05-09 18:00:00 30916.0
  27. ...
  28. ...
  29.  
  30. Since you are printing the actual output of your program, you
  31. can't print a debug statement without breaking the grader.
  32. Instead, you should use the logging module, which we've configured
  33. to log to a file which will be printed when you hit "Test Run".
  34. For example:
  35. logging.info("My debugging message")
  36. '''
  37.  
  38. max_entries = 0
  39. old_key = None
  40. datetime = ''
  41. oldtime=''
  42.  
  43. # your code here
  44. for line in sys.stdin:
  45. data=line.strip().split("\t")
  46.  
  47. if len(data)!=4:
  48. continue
  49. this_key, entry, date, time=data #unpack
  50.  
  51. if old_key and old_key!=this_key:
  52. print '{0}\t{1}\t{2}'.format(old_key, datetime, max_entries)
  53. max_entries=0
  54. datetime=''
  55. oldtime=time
  56.  
  57. old_key=this_key
  58. if max_entries < entry:
  59. max_entries = entry
  60. datetime=str(date)+' '+str(time)
  61.  
  62. if max_entries==entry:
  63. if time>oldtime:
  64. datetime=str(date)+' '+str(time)
  65. else:
  66. datetime=str(date)+' '+str(oldtime)
  67. if old_key!=None:
  68. print '{0}\t{1}\t{2}'.format(old_key, datetime, max_entries)
  69.  
  70.  
  71. reducer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement