def test_stack_push_one(): value = 1 stack = Stack() stack.push(value) actual = stack.top.value expected = value assert actual == expected
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_stack_pop(): value1 = 1 value2 = 2 stack = Stack() stack.push(value1) stack.push(value2) actual = stack.pop() expected = value2 assert actual == expected
def test_stack_push_many(): value1 = 1 value2 = 2 stack = Stack() stack.push(value1) stack.push(value2) actual = stack.top.value expected = value2 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_push_onto_full(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") actual = s.top.value expected = "cucumber" assert actual == expected
def test_pseudoqueue_dequeue_multiple(): testing_queue = PseudoQueue(Stack()) testing_queue.enqueue(1) testing_queue.enqueue(2) testing_queue.dequeue() expected = 2 actual = testing_queue.peek() assert actual == expected
def test_peek(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
def test_check_not_empty(): s = Stack() s.push("apple") s.push("banana") actual = s.is_empty() expected = False assert actual == expected
def test_is_empty(): stack = Stack() stack.push(1) stack.pop() actual = stack.is_empty() expected = True assert actual == expected
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_push_multiple(): stack = Stack() stack.push(1) stack.push(2) stack.push(3) actual = stack.top.value expected = 3 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_stack_new_empty(): stack = Stack() actual = stack.top expected = None assert actual == expected
def test_instantiate_stack(): stack = Stack() assert stack.top == None
def test_pop_one(): stack = Stack() stack.push(1) actual = stack.pop() expected = 1 assert actual == expected
def test_push_multiple_fails(): stack = Stack() stack.push(1) stack.push(2) stack.push(3) assert stack.top.value != 2
def test_push_onto_stack_not_none(): stack = Stack() stack.push(1) assert stack.top.value != None
def test_push_onto_stack(): stack = Stack() stack.push(1) actual = stack.top.value expected = 1 assert actual == expected
def __init__(self): self.back = Stack() self.front = Stack()
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 test_peek(): stack = Stack() stack.push(1) actual = stack.peek() expected = 1 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_stack_peek_empty_exception(): stack = Stack() with pytest.raises(Exception): stack.peek()
def __init__(self, stack1): self.stack1 = Stack() self.stack2 = Stack()
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 __init__(self): """Method initializer """ self.add = Stack() self.remove = Stack()
def __init__(self): self.main_stack = Stack() self.helper_stack = Stack()