Exemple #1
0
 def test_stack_linked4(self):
     stack = StackLinked()
     for i in range(10):
         stack.push(i)
     for i in range(10):
         val = stack.pop()
     self.assertRaises(IndexError, stack.pop)
     self.assertRaises(IndexError, stack.peek)
 def test_is_empty(self):
     stacka = StackArray()
     self.assertTrue(stacka.is_empty())
     stacka.num_items += 1
     self.assertFalse(stacka.is_empty())
     stackl = StackLinked()
     self.assertTrue(stackl.is_empty())
     stackl.num_items += 1
     self.assertFalse(stackl.is_empty())
Exemple #3
0
 def test_stack_linked5(self):
     stack = StackLinked()
     for i in range(3):
         stack.push(i)
         val = stack.pop()
         stack.push(val)
     self.assertEqual(stack.pop(), 2)
     self.assertEqual(stack.peek(), 1)
     self.assertEqual(stack.pop(), 1)
Exemple #4
0
 def test_stack_linked3(self):
     stack = StackLinked()
     for i in range(3):
         stack.push(i)
     self.assertEqual(stack.size(), 3)
     self.assertFalse(stack.is_empty())
     self.assertEqual(stack.pop(), 2)
     self.assertEqual(stack.pop(), 1)
     self.assertEqual(stack.pop(), 0)
     self.assertEqual(stack.size(), 0)
     self.assertTrue(stack.is_empty())
 def test_peek(self):
     stacka = StackArray()
     self.assertRaises(IndexError, stacka.peek)
     stacka.num_items = 2
     stacka.arr_list.arr = [1, 2]
     stacka.arr_list.num_items = 2
     self.assertEqual(stacka.peek(), 2)
     stackl = StackLinked()
     self.assertRaises(IndexError, stackl.peek)
     stackl.top = stacks.Node(1, None)
     stackl.num_items = 1
     self.assertEqual(stackl.peek(), 1)
Exemple #6
0
 def test_stack_linked2(self):
     stack = StackLinked()
     for i in range(3):
         stack.push(i)
     self.assertEqual(stack.size(), 3)
     self.assertFalse(stack.is_empty())
     self.assertEqual(stack.peek(), 2)
 def test_push(self):
     stack_1a = StackArray()
     stack_1a.push(7)
     stack_2a = StackArray()
     stack_2a.arr_list.arr = [7, None]
     stack_2a.arr_list.num_items = 1
     stack_2a.num_items = 1
     self.assertEqual(stack_1a, stack_2a)
     stack_1l = StackLinked()
     stack_1l.push(7)
     stack_2l = StackLinked()
     stack_2l.top = stacks.Node(7, None)
     stack_2l.num_items = 1
     self.assertEqual(stack_1l, stack_2l)
Exemple #8
0
 def dfs(self, vert, component):
     stack = StackLinked()
     while len(vert) > 0:
         vertex = vert[0]
         stack.push(vertex)
         self.get_vertex(vertex).visited = True
         vert.remove(vertex)
         lst = [vertex]
         while not stack.is_empty():
             current = self.get_vertex(stack.pop())
             for adj_vert in current.adjacent_to:
                 if not self.get_vertex(adj_vert).visited:
                     self.get_vertex(adj_vert).visited = True
                     lst.append(adj_vert)
                     stack.push(adj_vert)
                     vert.remove(adj_vert)
         component.append(sorted(lst))
     for i in self.graph:
         self.get_vertex(i).visited = False
     return component
Exemple #9
0
 def test_StackLinked(self):
     stack = StackLinked(3)
     self.assertRaises(IndexError, stack.pop)
     self.assertEqual(stack.is_empty(), True)
     stack.push('book1')
     self.assertEqual(stack.pop(), 'book1')
     stack.push('book1')
     stack.push('book2')
     self.assertEqual(stack.peek(), 'book2')
     self.assertEqual(stack.pop(), 'book2')
     self.assertEqual(stack.peek(), 'book1')
     stack.push('book2_2')
     stack.push('book3')
     self.assertRaises(IndexError, stack.push, 'book4')
     self.assertEqual(stack.size(), 3)
     self.assertEqual(stack.is_full(), True)
Exemple #10
0
 def test_stack_linked(self):
     list = StackLinked()
     list.push(5)
     list.push(10)
     list.push(12)
     list.push(13)
     print(list)
     list.pop()
     self.assertEqual(list.pop(), 12)
     print(list)
     self.assertEqual(list.peek(), 10)
     list1 = StackLinked()
     self.assertEqual(list1.peek(), None)
     with self.assertRaises(IndexError):
         list1.pop()
Exemple #11
0
def postfix_eval(postfix_expr):
    """evaluates the postfix expression
    Args:
        postfix_expr (str): the postfix expression to be evaluated
    Returns:
        int: the value of the evaluated expression
    Raises:
        SyntaxError: if the postfix expression is invalid
        ZeroDivisionError: if postfix expression has a divisor of 0
    """
    if postfix_valid(postfix_expr) is True:
        eval_stack = StackLinked()
        oper_list = postfix_expr.split()
        for item in oper_list:
            if item.isdigit():
                eval_stack.push(int(item))
            else:
                if item is "~":
                    oper1 = eval_stack.pop()
                    answer = -oper1
                    eval_stack.push(answer)
                else:
                    oper1 = eval_stack.pop()
                    oper2 = eval_stack.pop()
                    answer = evaluate(item, oper1, oper2)
                    eval_stack.push(answer)
        return eval_stack.pop()
    else:
        raise SyntaxError
Exemple #12
0
def infix_to_postfix(infix_expr):
    """converts an infix expression into a postfix expression
    Args:
        infix_expr (str): expression in infix notation only consisting of
                          numbers (integers, reals, positive or negative),
                          parentheses, and the operators separated by spaces.
    Returns:
        String: the postfix converted infix expression
    """
    operators = {"*": 3, "/": 3, "+": 2, "-": 2, "^": 4, "~": 4, "(": 1}
    operator_stack = StackLinked()
    post_fix = []
    infix = infix_expr.split()
    for i in infix:
        if is_number(i):
            post_fix.append(i)
        elif i == "(":
            operator_stack.push(i)
        elif i == ")":
            item = operator_stack.pop()
            while item != "(":
                post_fix.append(item)
                item = operator_stack.pop()
        else:
            while not operator_stack.is_empty() and \
                (operators[operator_stack.peek()] > operators[i]):
                post_fix.append(operator_stack.pop())
            operator_stack.push(i)
    while not operator_stack.is_empty():
        post_fix.append(operator_stack.pop())
    return " ".join(post_fix)
Exemple #13
0
 def test_stack_linked1(self):
     stack = StackLinked()
     self.assertEqual(stack.size(), 0)
     self.assertTrue(stack.is_empty())
Exemple #14
0
 def test_size(self):
     stk_a = StackLinked(4)
     stk_a.push(6)
     stk_a.push(7)
     stk_a.push(8)
     stk_a.push(9)
     self.assertTrue(stk_a.is_full())
     with self.assertRaises(IndexError): 
         stk_a.push(0)
     self.assertEqual(stk_a.size(), 4 )
     self.assertEqual(stk_a.pop(), 9 )
     self.assertEqual(stk_a.size(), 3 )
     self.assertFalse(stk_a.is_empty())
Exemple #15
0
 def test_push(self):
     stk_a = StackLinked(4)
     stk_a.push(3)
     self.assertEqual(stk_a.peek(), 3 )
     self.assertEqual(stk_a.pop(), 3 )
     self.assertTrue(stk_a.is_empty())
 def test_size(self):
     stacka = StackArray()
     self.assertEqual(stacka.size(), 0)
     stackl = StackLinked()
     stackl.num_items = 20
     self.assertEqual(stackl.size(), 20)
Exemple #17
0
def infix_to_postfix(infix_expr):
    """converts an infix expression to a postfix expression
    Args:
        infix_expr (str): the infix expression
    Returns:
        str: the postfix expression
    """
    postfix_list = []
    oper_stack = StackLinked()
    split_list = infix_expr.split()
    for item in split_list:
        if item in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or item.isdigit():
            postfix_list.append(item)
        elif item == '(':
            oper_stack.push(item)
        elif item == ')':
            top = oper_stack.pop()
            while top != '(':
                postfix_list.append(top)
                top = oper_stack.pop()
        elif item == "^" and oper_stack.peek() == "~":
            oper_stack.push(item)
        elif item == "~" and oper_stack.peek() == "~":
            oper_stack.push(item)
        else:
            while (not oper_stack.is_empty()) and \
                   (oper[oper_stack.peek()] >= oper[item]):
                postfix_list.append(oper_stack.pop())
            oper_stack.push(item)
    while not oper_stack.is_empty():
        postfix_list.append(oper_stack.pop())
    return " ".join(postfix_list)
Exemple #18
0
def postfix_eval(postfix_expr):
    """evaluates a postfix expression into a value. Uses the postfix_valid function described
       below to check the validity of the expression
    Args:
        postfix_expr (str): an expression in postfix notation
    Returns:
        int/float: the solution to the postfix expression if valid
    Raises:
        SyntaxError: when the postfix expression is invalid
        ZeroDivisionError: when dividing by zero
    """
    if not postfix_valid(postfix_expr):
        raise SyntaxError
    expr = postfix_expr.split()
    operands_stack = StackLinked()
    for i in expr:
        if is_number(i):
            operands_stack.push(float(i))
        elif i == "^":
            exp = operands_stack.pop()
            item = operands_stack.pop() ** exp
            operands_stack.push(item)
        elif i == "*":
            item = operands_stack.pop() * operands_stack.pop()
            operands_stack.push(item)
        elif i == "/":
            div = operands_stack.pop()
            if div == 0:
                raise ZeroDivisionError
            item = operands_stack.pop() / div
            operands_stack.push(item)
        elif i == "+":
            item = operands_stack.pop() + operands_stack.pop()
            operands_stack.push(item)
        elif i == "-":
            sub2 = operands_stack.pop()
            item = operands_stack.pop() - sub2
            operands_stack.push(item)
        elif i == "~":
            item = operands_stack.pop() * -1
            operands_stack.push(item)
    return round(operands_stack.pop(), 2)