class MyStack(object): def __init__(self): """ Initialize your data structure here. """ self.input_queue = Queue() self.output_queue = Queue() def push(self, x): """ Push element x onto stack. :type x: int :rtype: None """ self.input_queue.push(x) def pop(self): """ Removes the element on top of the stack and returns that element. :rtype: int """ size = self.input_queue.size() if size == 0: return None for i in range(size - 1): self.output_queue.push((self.input_queue.pop())) item = self.input_queue.pop() self.input_queue, self.output_queue = self.output_queue, self.input_queue return item def top(self): """ Get the top element. :rtype: int """ size = self.input_queue.size() if size == 0: return None for i in range(size - 1): self.output_queue.push((self.input_queue.pop())) item = self.input_queue.peek() self.output_queue.push((self.input_queue.pop())) self.input_queue, self.output_queue = self.output_queue, self.input_queue return item def empty(self): """ Returns whether the stack is empty. :rtype: bool """ return self.input_queue.is_empty()
class TestQueue(unittest.TestCase): @classmethod def setUpClass(self): self.initial_list = [i for i in range(5)] @classmethod def tearDownClass(self): print("All tests for queue completed") def setUp(self): # print("Initializing queue") self.queue = Queue(8, 0, initial_iter=self.initial_list) def tearDown(self): pass def test_head(self): """ Test getting top element """ self.assertEqual(4, self.queue.head()) def test_tail(self): """ Test getting top element """ self.assertEqual(0, self.queue.tail()) def test_enqueue(self): """ Test pushing to stack top """ self.queue.enqueue(5) self.assertEqual(5, self.queue.tail()) self.assertNotEqual(5, self.queue.head()) self.assertEqual(4, self.queue.head()) self.assertEqual(6, self.queue.size()) def test_dequeue(self): """ Test popping """ for x in range(4, -1, -1): self.assertEqual(x, self.queue.dequeue()) self.assertEqual(0, self.queue.size())