Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- class Clicker:
- def __init__(self):
- self.clicks = [0]*3600 # ring buffer of clicks in the last hour
- self.lastClick = int(time.time())
- def _scrub(self):
- now = int(time.time())
- c_idx = now % 3600
- if now - self.lastClick > 3600:
- self.clicks = [0]*3600
- elif now != self.lastClick:
- l_idx = self.LastClick() % 3600
- for i in range(3600):
- t_idx = (l_idx + i) % 3600
- self.clicks[t_idx] = 0
- if t_idx == c_idx:
- break
- return c_idx
- def clickCountLastHour(self):
- # return the sum of ring buffer
- cnt = 0
- self._scrub()
- for c in self.clicks:
- cnt += c
- return cnt
- def click(self):
- c_idx = self._scrub()
- self.clicks[c_idx] += 1
Add Comment
Please, Sign In to add comment