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 test_peek_one(): 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 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
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_peek(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') stack.push('Andy') stack.peek() actual = stack.top.value expected = 'Andy' assert actual == expected
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()
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_push_many(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') actual = stack.top.value expected = 'Jim' 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_pop_some(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() actual = s.pop() expected = "banana" 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_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
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_single(): s = Stack() s.push("apple") actual = s.pop() expected = "apple" assert actual == expected
def test_peek(): s = Stack() s.push("apple") actual = s.peek() expected = "apple" assert actual == expected
def test_push(): stack = Stack() stack.push('Dwight') actual = stack.top.value expected = 'Dwight' assert actual == expected
def test_push_onto_empty(): s = Stack() s.push("apple") actual = s.top.value excepted = "apple" assert actual == excepted