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()
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_is_empty(): stack = Stack() stack.push(1) stack.pop() actual = stack.is_empty() expected = True assert actual == expected
class Pseudoqueue: def __init__(self): self.back = Stack() self.front = Stack() def enqueue(self, value): print("pushing ", value) try: while self.front.top != None: self.back.push(self.front.pop()) # print('xxxx') # print(self.back.top.value) # print(self.back.top.next) except AttributeError: print('Front is Empty') self.back.push(value) # print(self.back.top.value) try: while self.back.top != None: # print(self.back.top.value) self.front.push(self.back.pop()) # print('-----------') except AttributeError: print('Back is Empty') # print('top: ', self.front.top.value) def dequeue(self): return self.front.pop()
def test_pop_when_multiple(): stack = Stack() stack.push(1) stack.push(2) stack.push(3) stack.pop() stack.pop() actual = stack.pop() expected = 1 assert actual == expected
def test_stack_pop_until_empty(): value1 = 1 value2 = 2 stack = Stack() stack.push(value1) stack.push(value2) stack.pop() stack.pop() actual = stack.top expected = None assert actual == expected
def test_pop_until_empty(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() s.pop() s.pop() actual = s.is_empty() expected = True assert actual == expected
def test_pop_some(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() actual = s.pop() expected = "banana" assert actual == expected
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')
def multi_bracket_validation(input: str) -> bool: """Check if the passed string has matching pairs of brackets Args: input (str): String to be checked Returns: bool: Whether or not the brackets match """ br_stack = Stack() brackets = { ')': '(', ']': '[', '}': '{', } for char in input: # Add opening bracket to the stack if char in brackets.values(): br_stack.push(char) # Check if closing bracket matches the most recent opening elif char in brackets.keys(): try: if not brackets[char] == br_stack.pop(): return False except AttributeError as err: print(err) return False return br_stack.is_empty()
def test_stack_pop(): value1 = 1 value2 = 2 stack = Stack() stack.push(value1) stack.push(value2) actual = stack.pop() expected = value2 assert actual == expected
def test_raise_exception_when_popping(): stack = Stack() with pytest.raises(InvalidOperationError) as e: stack.pop() assert str(e.value) == "Popping from an empty stack!"
def test_pop_one(): stack = Stack() stack.push(1) actual = stack.pop() expected = 1 assert actual == expected
def test_stack_pop_empty_exception(): stack = Stack() with pytest.raises(Exception): stack.pop()
def test_pop_empty(): s = Stack() with pytest.raises(InvalidOperationError) as e: s.pop() assert str(e.value) == "Method not allowed on empty collection"
def test_pop_single(): s = Stack() s.push("apple") actual = s.pop() expected = "apple" assert actual == expected