-
Notifications
You must be signed in to change notification settings - Fork 3
/
105_Facebook_Debounce_function.py
executable file
·71 lines (46 loc) · 1.76 KB
/
105_Facebook_Debounce_function.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Good morning! Here's your coding interview problem for today.
This problem was asked by Facebook.
Given a function f, and N return a debounced f of N milliseconds.
That is, as long as the debounced f continues to be invoked, f itself will not be called for N milliseconds.
"""
# great visualization : http://demo.nimius.net/debounce_throttle/
# decorators : https://www.datacamp.com/community/tutorials/decorators-python
# source: https://jonlabelle.com/snippets/view/python/python-debounce-decorator-function
from threading import Timer
import time # only for testing
def debounce(wait):
"""
As long as the function is being called don't execute it
after the function call has ended execute the function after N seconds.
Args:
wait (int): wait time
"""
def decorator(func):
def wrapper(*args, **kwargs):
def call_it():
func(*args, **kwargs)
try:
decorator.debounced_func.cancel()
except AttributeError:
pass
decorator.debounced_func = Timer(wait, call_it)
decorator.debounced_func.start()
return wrapper
return decorator
@debounce(1)
def print_symbol(symbol):
print(symbol, end="")
if __name__ == '__main__':
# so the print_symbol function will be executed after its not being called anymore,
# it will use the latest call
for _ in range(20):
print_symbol("*")
print("-", end="")
time.sleep(3) # sleep for 2 secs before running the next loop
print("\n")
symbols = ['*', '%', '$']
# 0 1 2 3 4 5 6 7 8
# for range 5(i.e 0-4) it should print '%' -> * % $ * % $
for i in range(5):
print_symbol(symbols[i%3])