Guest User

Untitled

a guest
Mar 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import time
  2.  
  3. class Clicker:
  4. def __init__(self):
  5. self.clicks = [0]*3600 # ring buffer of clicks in the last hour
  6. self.lastClick = int(time.time())
  7.  
  8.  
  9. def _scrub(self):
  10. now = int(time.time())
  11. c_idx = now % 3600
  12. if now - self.lastClick > 3600:
  13. self.clicks = [0]*3600
  14. elif now != self.lastClick:
  15. l_idx = self.LastClick() % 3600
  16. for i in range(3600):
  17. t_idx = (l_idx + i) % 3600
  18. self.clicks[t_idx] = 0
  19. if t_idx == c_idx:
  20. break
  21. return c_idx
  22.  
  23. def clickCountLastHour(self):
  24. # return the sum of ring buffer
  25. cnt = 0
  26. self._scrub()
  27. for c in self.clicks:
  28. cnt += c
  29. return cnt
  30.  
  31. def click(self):
  32. c_idx = self._scrub()
  33. self.clicks[c_idx] += 1
Add Comment
Please, Sign In to add comment