about summary refs log tree commit diff
path: root/scratch/facebook/interview-cake/queue-two-stacks.py
blob: bfa465f98d7f613d3a600d8a2924974cdff675f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Queue(object):
    def __init__(self):
        self.lhs = []
        self.rhs = []

    def enqueue(self, x):
        self.lhs.append(x)

    def dequeue(self):
        if self.rhs:
            return self.rhs.pop()
        while self.lhs:
            self.rhs.append(self.lhs.pop())
        if self.rhs:
            return self.rhs.pop()
        else:
            raise Exception("Attempting to remove an item from an empty queue")