-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentCounter.py
More file actions
28 lines (24 loc) · 916 Bytes
/
RecentCounter.py
File metadata and controls
28 lines (24 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#instance method here or a function defined inside a class
from collections import deque
class RecentCounter:
#constructor
def __init__(self):
self.queue = deque()
def ping(self, t):
#need to add one to the queue otherwise is empty
#when element is less than t-3000 popleft
#and we continue adding here
self.queue.append(t)
while self.queue[0] < t-3000:
#as long as the condition is true
#we keep returning the length of self.queue
#because they all fit the condition
#until 3002 which fails the condition
#and gets popped
self.queue.popleft()
return len(self.queue)
myVar = RecentCounter()
param1 = myVar.ping(1) #calling ping with t param
param2 = myVar.ping(100)
param3 = myVar.ping(3001)
param4 = myVar.ping(3002)