def is_balanced(string):
    """Check if string of parenthesis is valid."""
    """Reverse string and use stack, in accordance with assignment."""
    rev = string[::-1]
    parens = Stack()

    for i in rev:
        parens.push(i)

    count = 0

    for i in range(parens.__len__()):
        p = parens.pop()

        if p == '(':
            count += 1
        elif p == ')':
            count -= 1

        if count < 0:
            break

    if count == 0:
        return 'balanced'
    elif count >= 1:
        return 'open'
    else:
        return 'broken'
def is_paranthesis_balanced(s):

    open_bkt = ['(', '[', '{', '<']
    matches = [('{', '}'), ('[', ']'), ('(', ')'), ('<', '>')]
    stack = Stack(len(s))

    for i in s:
        if i in open_bkt:
            stack.push(i)
        else:
            if stack.__len__() == 0:
                return False
            last_bkt = stack.pop()
            if (last_bkt, i) not in matches:
                return False
    return stack.__len__() == 0
Beispiel #3
0
 def dft_print(self, node):
     stack = Stack()
     if self:
         stack.push(self)
     while stack.__len__() > 0:
         current_node = stack.pop()
         print(current_node.value)
         if current_node.right != None:
             stack.push(current_node.right)
         if current_node.left != None:
             stack.push(current_node.left)
    def dft_print(self, node):
        dft_stack = Stack()
        dft_stack.push(node)

        while dft_stack.__len__() != 0:
            current_node = dft_stack.pop()
            if current_node.left is not None:
                dft_stack.push(current_node.left)
            if current_node.right is not None:
                dft_stack.push(current_node.right)
            print(current_node.value)
Beispiel #5
0
    def dft_print(self, node):
        # Set up the stack
        the_stack = Stack()
        the_stack.push(node)

        while the_stack.__len__() > 0:  # While there is anything in the stack
            current_node = the_stack.pop()  # Pop off the top of the stack
            print(current_node.value)  # Print the value of the popped node
            if current_node.left:  # Check if there's a left branch
                the_stack.push(current_node.left)  # Add it to the stack
            if current_node.right:  # Check if there's a right branch
                the_stack.push(current_node.right)  # Add it to the stack
Beispiel #6
0
    def test_length(self):

        # Arrange
        my_stack = Stack()
        my_stack.push(1)
        my_stack.push(2)

        # Act
        len1 = my_stack.__len__()

        # Assert
        self.assertEqual(2, len1, "length did not have the expected value")
Beispiel #7
0
 def dft_print(self, node):
     # create a stack for nodes
     stack = Stack()
     # add the first node to the stack
     stack.push(node)
     # while the stack is not empty
     while stack.__len__() != 0:
         # get the current node from the top of the stack
         removed = stack.pop()
         # print that node
         print(removed.value)
         # add all children to the stack
         if removed.right is not None:
             stack.push(removed.right)
         if removed.left is not None:
             stack.push(removed.left)
 def dft_print(self, node):
     # Initialize empty stack
     # Push the root node onto the stack
     stack = Stack()
     stack.push(node)
     # Need a while loop to manage iteration
     # If stack is not empty enter the while loop
     while stack.__len__() > 0:
         # pop top item off stack
         # print items value
         current = stack.pop()
         print(current.value)
         # if there is left subtree
         if current.left is not None:
             stack.push(current.left)
             # push left item onto stack
         # if there is right subtree
         if current.right is not None:
             stack.push(current.right)
Beispiel #9
0
    def dequeue(self):
        self.size -= 1
        if not self.head:
            return None
        else:
            value = self.head.get_value()
            self.head = self.head.get_next()
            return value


print(Stack)
b = Stack()

l = Queue()
b.push(100), "Length:", b.__len__()
b.push(101), "Length:", b.__len__()
b.push(102), "Length:", b.__len__()
b.push(103), "Length:", b.__len__()

l.enqueue(100)
l.enqueue(101)
l.enqueue(105)
l.enqueue(4)

print(l.dequeue(), "Length:", l.__len__())
print(l.dequeue(), "Length:", l.__len__())
print(l.dequeue(), "Length:", l.__len__())
print(l.dequeue(), "Length:", l.__len__())
print(l.dequeue(), "Length:", l.__len__())
Beispiel #10
0
def test_len():
    s = Stack()
    value = s.__len__()
    assert value == 0