logger_rate_limiter

 1# @leet start
 2class Logger:
 3    """
 4    This logger class implements a logger that debounces logging calls.
 5    The first time the logger sees a message, it decides to log it, and count for 10 seconds
 6    since it last saw that message.
 7    If it has seen the message within 10 seconds, it doesn't log the message.
 8
 9    To keep track of everything, we can create a dictionary that maps key -> last logged timestamp.
10    Then, when a new message comes along, if it either doesn't exist in the dictionary or
11    it hasn't been logged for at least 10 seconds, we set the message's new timestamp and log the message.
12    Otherwise, no logging is done.
13    """
14    def __init__(self):
15        self.times = {}
16
17    def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
18        if message not in self.times or self.times[message] + 10 <= timestamp:
19            self.times[message] = timestamp
20            return True
21        else:
22            return False
23
24def test():
25	assert(2 + 2 == 4)
class Logger:
 3class Logger:
 4    """
 5    This logger class implements a logger that debounces logging calls.
 6    The first time the logger sees a message, it decides to log it, and count for 10 seconds
 7    since it last saw that message.
 8    If it has seen the message within 10 seconds, it doesn't log the message.
 9
10    To keep track of everything, we can create a dictionary that maps key -> last logged timestamp.
11    Then, when a new message comes along, if it either doesn't exist in the dictionary or
12    it hasn't been logged for at least 10 seconds, we set the message's new timestamp and log the message.
13    Otherwise, no logging is done.
14    """
15    def __init__(self):
16        self.times = {}
17
18    def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
19        if message not in self.times or self.times[message] + 10 <= timestamp:
20            self.times[message] = timestamp
21            return True
22        else:
23            return False

This logger class implements a logger that debounces logging calls. The first time the logger sees a message, it decides to log it, and count for 10 seconds since it last saw that message. If it has seen the message within 10 seconds, it doesn't log the message.

To keep track of everything, we can create a dictionary that maps key -> last logged timestamp. Then, when a new message comes along, if it either doesn't exist in the dictionary or it hasn't been logged for at least 10 seconds, we set the message's new timestamp and log the message. Otherwise, no logging is done.

times
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
18    def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
19        if message not in self.times or self.times[message] + 10 <= timestamp:
20            self.times[message] = timestamp
21            return True
22        else:
23            return False
def test():
25def test():
26	assert(2 + 2 == 4)