-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path359-Logger-Rate-Limiter.py
27 lines (18 loc) · 1.03 KB
/
359-Logger-Rate-Limiter.py
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
'''
Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).
All messages will come in chronological order. Several messages may arrive at the same timestamp.
Implement the Logger class:
Logger() Initializes the logger object.
bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.
'''
class Logger:
def __init__(self):
self.hash = {}
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
if(message not in self.hash or self.hash[message] + 10 <= timestamp):
self.hash[message] = timestamp
return True
return False
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)