def test_stack(): test_stack = Stack() """helper function to create a stack""" test = [1, 2, 3, 4] for el in test: test_stack.push(el) return test_stack
def test_pop(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') stack.pop() actual = stack.top.value expected = 'Michael' assert actual == expected
def test_push_onto_full(): s = Stack() s.push("apple") s.push("cucumber") s.push("phone") actual = s.top.value excepted = "phone" assert actual == excepted
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_check_not_empty(): s = Stack() s.push("apple") s.push("banana") actual = s.is_empty() expected = False assert actual == expected
def test_peek_one(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
def walk(root, stack): # record holders for heaviest leaf, and corresponding weight nonlocal heaviest_leaf nonlocal heaviest_weight if not stack: stack = Stack() if root.left: left = self.walk(root.left, stack) if root.right: right = self.walk(root.right, stack) # are we at a leaf node (a node with no children)? if not root.left and not root.right: # if we've not encountered any leaf yet, or our current leaf # is the new heaviest leaf, lets set our record holders if heaviest_weight is None or (root.value + left + right > heaviest_weight): heaviest_leaf = root heaviest_weight = root.value + left + right stack.push(root) else: stack.pop()
def validate_string(string): stack = Stack() # opening symbols opening = '{[(' # closing symbols, must be in the matched order of opening closing = '}])' # iterate through the string we are testing for char in string: # if we encounter an opening symbol, push it to the stack if char in opening: stack.push(char) # if we encounter a closing symbol, check if it matches the top of the stack elif char in closing: i = closing.index(char) if stack.is_empty() or stack.pop() != opening[i]: return False # if we've not matched all the symbols... fail! if not stack.is_empty: return False # we have matched all the symbols, success! return True
class PsuedoQueue: """ This is a queue being implement using stack's push, pop and peek methods rather than having its own attributes and methods """ def __init__(self): self.front_stack = Stack() self.rear_stack = Stack() # this is strictly here so that we may pass a test, self.front = None def peek(self): return self.front_stack.peek() def is_empty(self): return self.front_stack.is_empty() def enqueue(self, value=None): node = Node(value) if self.rear_stack.top: self.rear_stack.top.next = node self.rear_stack.top = node if not self.front_stack.top: self.front_stack.top = node # next line is solely here to pass a test self.front = self.front_stack.top def dequeue(self): # if the queue only holds one node, we must manually set the rear to None if (self.rear_stack.top == self.front_stack.top): self.rear_stack.top = None value = self.front_stack.pop() # next line is solely here to pass a test self.front = self.front_stack.top return value
class PseudoQueue(): def __init__(self): self.input = Stack() self.output = Stack() def enqueue(self, value): self.input.push(value) def dequeue(self): # Empty the input stack into output stack if self.input: try: value = self.input.pop() self.output.push(value) except Exception: return("Unable to successfully dequeue") # Return top of the output stack return self.output.pop()
class PseudoQueue: """Creates instance of Queue using stacks to track front and rear of queue """ def __init__(self) -> object: """PseudoQueue instance. front and back are represented by stack objects """ self.front = Stack() self.rear = Stack() def __str__(self) -> str: """String Literal showing PseudoQueue instance current front and back """ return f'A PseudoQueue instance current front value: {self.front.peek()}current rear value: {self.rear.peek()}' def enqueue(self, value): """Adds a node to the PseudoQueue instance input <-- any output --> non-fruitful (mutates instance of PseudoQueue in place) """ if not self.front.top: self.front.push(value) self.rear.top = self.front.top else: curr_rear = self.rear.top self.rear.push(value) curr_rear.next = self.rear.top def dequeue(self): if self.front.top.next == None: raise InvalidOperationError( 'Method not allowed on an empty collection') else: new_front = self.front.top.next temp = self.front.pop() self.front.push(new_front.value) return temp
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_is_empty(): s = Stack() actual = s.isEmpty() expected = True assert actual == expected
def test_peek(): s = Stack() s.push("apple") actual = s.peek() expected = "apple" assert actual == expected
def test_pop_till_empty(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') stack.pop() stack.pop() stack.pop() actual = stack.is_empty() expected = True assert actual == expected
def test_push_onto_empty(): s = Stack() s.push("apple") actual = s.top.value excepted = "apple" assert actual == excepted
def test_peek(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') stack.push('Andy') stack.peek() actual = stack.top.next.value expected = 'Jim' 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_is_stack(): assert Stack()
def test_empty_stack(): stack = Stack() assert stack.is_empty()
def test_stack_has_top(): s = Stack() assert s.top is None
def __init__(self): self.input = Stack() self.output = Stack()
def test_push(): stack = Stack() stack.push('Dwight') actual = stack.top.value expected = 'Dwight' assert actual == expected
def test_peek_on_empty(): new_stack = Stack() with pytest.raises(InvalidOperationError) as e: new_stack.peek()
def test_pop_single(): s = Stack() s.push("apple") actual = s.pop() expected = "apple" 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
def __init__(self): self.front_stack = Stack() self.rear_stack = Stack() # this is strictly here so that we may pass a test, self.front = None
def test_new_empty_stack(): stack = Stack() actual = stack.top expected = None assert actual == expected
def __init__(self) -> object: """PseudoQueue instance. front and back are represented by stack objects """ self.front = Stack() self.rear = Stack()
def test_stack_is_empty(test_stack): embty_stack = Stack() assert embty_stack.isEmpty() == True assert test_stack.isEmpty() == False