예제 #1
0
        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()
예제 #2
0
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_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
예제 #5
0
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 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 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()
      
예제 #8
0
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
예제 #9
0
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
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():
    s = Stack()
    s.push("apple")
    actual = s.pop()
    expected = "apple"
    assert actual == expected