aboutsummaryrefslogtreecommitdiff
path: root/print_foobar_alternately.py
blob: d8b121aff8bccd401733ee8cc7638674785e6718 (plain)
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
# 1115. Print FooBar Alternately
from threading import Semaphore

"""
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()' and thread 'b' will call 'bar()'. modify the given
program to output "foobar" 'n' times.
"""


def printFoo():
    print("foo")


def printBar():
    print("bar")


class FooBar(object):
    def __init__(self, n):
        self.n = n
        self.s_foo = Semaphore(1)
        self.s_bar = Semaphore(0)

    def foo(self, printFoo):
        """
        :type printFoo: method
        :rtype: void
        """
        for i in range(self.n):
            self.s_foo.acquire()
            # printFoo() outputs "foo". Do not change or remove this line.
            printFoo()
            self.s_bar.release()

    def bar(self, printBar):
        """
        :type printBar: method
        :rtype: void
        """
        for i in range(self.n):
            self.s_bar.acquire()
            # printBar() outputs "bar". Do not change or remove this line.
            printBar()
            self.s_foo.release()


if __name__ == "__main__":
    f1 = FooBar(1)
    f2 = FooBar(2)
    f1.foo()
    f1.bar()
    f2.foo()
    f2.bar()