class PseudoQueue: """Queue that uses two stacks""" def __init__(self): """Method initializer """ self.add = Stack() self.remove = Stack() def __len__(self): return len(self.add) + len(self.remove) def enqueue(self, val): """Add a new element ot the queue Args: val (any): Value to be added Returns: any: Value that was added to the queue """ try: while self.remove.peek(): self.add.push(self.remove.pop()) except AttributeError as err: pass self.add.push(val) return self.add.top.val def dequeue(self): """Remove the first element from the queue and return its value Raises: AttributeError: When the method is called on the empty queue Returns: any: Value of the removed element """ try: while self.add.peek(): self.remove.push(self.add.pop()) except AttributeError as err: pass try: return self.remove.pop() except AttributeError as err: raise AttributeError('Cannot be called on empty queue')
class PseudoQueue: def __init__(self, stack1): self.stack1 = Stack() self.stack2 = Stack() def enqueue(self, value): self.stack1.push(value) def dequeue(self): if self.stack1.top == None: raise EmptyQueueException() while self.stack1.top != None: popped_value = self.stack1.pop() self.stack2.push(popped_value) stack2_popped = self.stack2.pop() while self.stack2.top != None: self.stack1.push(self.stack2.pop()) return stack2_popped def peek(self): return self.stack1.peek()
def test_stack_peek(): value = 1 node = Node(value) stack = Stack(node) actual = stack.peek() expected = value assert actual == expected
def test_peek(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
class AnimalShelter: def __init__(self): self.main_stack = Stack() self.helper_stack = Stack() def enqueue(self, animal): while not isinstance(self.main_stack.peek(), EmptyStackException): self.helper_stack.push(self.main_stack.pop()) self.helper_stack.push(animal) while not isinstance(self.helper_stack.peek(), EmptyStackException): self.main_stack.push(self.helper_stack.pop()) def dequeue(self, animal): if isinstance(self.main_stack.peek(), EmptyStackException): return EmptyStackException() saved_animal = None while not isinstance(self.main_stack.peek(), EmptyStackException): if not saved_animal and isinstance(self.main_stack.peek(), animal): saved_animal = self.main_stack.pop() else: self.helper_stack.push(self.main_stack.pop()) while not isinstance(self.helper_stack.peek(), EmptyStackException): self.main_stack.push(self.helper_stack.pop()) return saved_animal def peek(self): return self.main_stack.peek()
def test_raise_exception_when_peeking(): stack = Stack() with pytest.raises(InvalidOperationError) as e: stack.peek() assert str(e.value) == "Peeking from an empty stack!"
def test_peek(): stack = Stack() stack.push(1) actual = stack.peek() expected = 1 assert actual == expected
def test_stack_peek_empty_exception(): stack = Stack() with pytest.raises(Exception): stack.peek()
def test_peek_empty(): s = Stack() with pytest.raises(InvalidOperationError) as e: s.peek() assert str(e.value) == "Method not allowed on empty collection"