-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1115 Print FooBar Alternately.py
60 lines (47 loc) · 1.4 KB
/
1115 Print FooBar Alternately.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
#!/usr/bin/python3
"""
Suppose you are given the following code:
class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print("foo");
}
}
public void bar() {
for (int i = 0; i < n; i++) {
print("bar");
}
}
}
The same instance of FooBar will be passed to two different threads. Thread A
will call foo() while thread B will call bar(). Modify the given program to
output "foobar" n times.
Example 1:
Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls
foo(), while the other calls bar(). "foobar" is being output 1 time.
Example 2:
Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times.
"""
from threading import Lock
from typing import Callable
class FooBar:
def __init__(self, n):
self.n = n
self.locks = [Lock(), Lock()]
self.locks[1].acquire()
def foo(self, printFoo: Callable[[], None]) -> None:
for i in range(self.n):
self.locks[0].acquire()
# printFoo() outputs "foo". Do not change or remove this line.
printFoo()
self.locks[1].release()
def bar(self, printBar: Callable[[], None]) -> None:
for i in range(self.n):
self.locks[1].acquire()
# printBar() outputs "bar". Do not change or remove this line.
printBar()
self.locks[0].release()