about summary refs log tree commit diff
path: root/users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py
diff options
context:
space:
mode:
Diffstat (limited to 'users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py')
-rw-r--r--users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py b/users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py
new file mode 100644
index 000000000000..bfa465f98d7f
--- /dev/null
+++ b/users/wpcarro/scratch/facebook/interview-cake/queue-two-stacks.py
@@ -0,0 +1,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")