Ejemplo n.º 1
0
 def test_stack3(self):
     stack = StackLinkedList()
     self.assertTrue(stack.is_empty())
     stack.push(1)
     self.assertFalse(stack.is_empty())
     item = stack.pop()
     self.assertEqual(item, 1)
Ejemplo n.º 2
0
 def test_stack(self):
     # initialize an object
     s = StackLinkedList()
     self.assertEqual(s.is_empty(), True)
Ejemplo n.º 3
0
 def __init__(self, infix):
     self.tokens = infix.split()
     self.opStack = StackLinkedList()
     self.valStack = StackLinkedList()
     self.parentheses = 0
Ejemplo n.º 4
0
 def test_stack2(self):
     # initialize an object
     s = StackLinkedList()
     s.push(1)
     self.assertEqual(s.is_empty(), False)
Ejemplo n.º 5
0
class DijkstraTwoStack:
    def __init__(self, infix):
        self.tokens = infix.split()
        self.opStack = StackLinkedList()
        self.valStack = StackLinkedList()
        self.parentheses = 0

    def process_token(self, s):
        if s == "(":
            self.parentheses += 1
        elif s == ")":
            self.parentheses -= 1
            self.operate()
        elif s in "+ - * / ^ %".split():
            self.opStack.push(s)
        else:
            n = int(s)
            self.valStack.push(n)

    def operate(self):
        y = self.valStack.pop()
        x = self.valStack.pop()

        if self.opStack.peek() == "+":
            self.valStack.push(x + y)
            self.opStack.pop()
            return
        elif self.opStack.peek() == "-":
            self.valStack.push(x - y)
            self.opStack.pop()
            return
        elif self.opStack.peek() == "*":
            self.valStack.push(x * y)
            self.opStack.pop()
            return
        elif self.opStack.peek() == "/":
            self.valStack.push(x / y)
            self.opStack.pop()
            return
        else:
            raise BqsException(
                f"operator not recognized: {self.opStack.peek()}")

    def evaluate(self):
        for i in self.tokens:
            self.process_token(i)

        if self.parentheses != 0:
            raise BqsException(
                f"there are {self.parentheses} superfluous parentheses (net)")

        while not self.opStack.is_empty():
            self.operate()

        result = self.valStack.pop()

        if not self.valStack.is_empty():
            raise BqsException("there are superfluous values")

        return result