forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_105.py
37 lines (28 loc) · 838 Bytes
/
problem_105.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
28
29
30
31
32
33
34
35
36
37
# source - https://gist.github.com/kylebebak/ee67befc156831b3bbaa88fb197487b0
import time
def debounce(s):
"""
Decorator ensures function that can only be called once every `s` seconds.
"""
interval = s * (10**(-3))
def decorate(f):
current_time = None
def wrapped(*args, **kwargs):
nonlocal current_time
start_time = time.time()
if current_time is None or start_time - current_time >= interval:
result = f(*args, **kwargs)
current_time = time.time()
return result
return wrapped
return decorate
@debounce(3000)
def add_nums(x, y):
return x + y
assert add_nums(1, 1) == 2
time.sleep(1)
assert not add_nums(1, 2)
time.sleep(1)
assert not add_nums(1, 3)
time.sleep(1)
assert add_nums(1, 4) == 5