aboutsummaryrefslogtreecommitdiff
path: root/front_middle_back_queue.py
blob: cc3e619de0d0156ee92701d91b4fc5eff404c7da (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
# 1670. Design Front Middle Back Queue

"""
design a queue that supports 'push' and 'pop' operations in the front,
middle, and back. notice that when there are two middle position choices, the
operation is performed on the frontmost middle position choice.
"""


class FrontMiddleBackQueue(object):
    def __init__(self):
        self.a = []

    def pushFront(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.a.insert(0, val)

    def pushMiddle(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.a.insert(len(self.a) / 2, val)

    def pushBack(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.a.append(val)

    def popFront(self):
        """
        :rtype: int
        """
        return self.a or [-1].pop(0)

    def popMiddle(self):
        """
        :rtype: int
        """
        return self.a or [-1].pop((len(self.a) - 1) / 2)

    def popBack(self):
        """
        :rtype: int
        """
        return self.a or [-1].pop()


if __name__ == "__main__":
    obj = FrontMiddleBackQueue()
    obj.pushFront(1)
    obj.pushBack(2)
    obj.pushMiddle(3)
    obj.pushMiddle(4)
    print(obj.popFront())
    print(obj.popMiddle())
    print(obj.popMiddle())
    print(obj.popBack())
    print(obj.popFront())