Пример #1
0
def infix_to_postfix(input_str):
    x = input_str.split(" ")
    stack = stack_array.Stack(len(x))
    ex = ""
    for i in x:  #Operators
        print(i, stack.items, ex)
        if is_operator(i):
            #print(i, stack.items)
            if (stack.is_empty() == False) and ((stack.items[0] != ")") and
                                                (stack.items[0] != "(")):
                while (precedence(stack.items[0]) >= precedence(i) and
                       (stack.items[0] != "**")) or (
                           (precedence(stack.items[0]) > precedence(i)) and
                           (stack.items[0] == "**")):
                    ex += stack.pop() + " "
            stack.push(i)
        elif i == "(":  #Opening Parenthesis
            stack.push(i)
        elif i == ")":  #Closing Parenthesis
            c = None
            while c != "(":
                c = stack.pop()
                if (c != "(") and (c != ")"):
                    ex += c + " "
        else:  #Values
            ex += i + " "
    for i in range(stack.size()):
        ex += stack.pop() + " "
    exx = ex[0:-1]
    return exx
Пример #2
0
 def test_array_size(self):
     stackT = stack_array.Stack(6)
     stackT.push(5)
     stackT.push(1)
     stackT.push(3)
     stackT.pop()
     stackT.push(7)
     stackT.push(9)
     self.assertEqual(stackT.size(), 4)
Пример #3
0
 def test_array_pop_2(self):
     stackT = stack_array.Stack(4)
     stackT.push(4)
     stackT.push(5)
     stackT.push("hello")
     self.assertEqual(stackT.pop(), "hello")
     stackT.push(8)
     self.assertEqual(stackT.pop(), 8)
     self.assertEqual(stackT.pop(), 5)
Пример #4
0
def postfix_eval(input_str):
    if len(input_str) == 0:
        raise PostfixFormatException("Insufficient operands")
    x = input_str.split(" ")
    stack = stack_array.Stack(len(x))
    for i in x:  #Operators
        if is_operator(i):
            if (stack.items[0] == None) or (stack.items[1] == None):
                raise PostfixFormatException("Insufficient operands")
            if i == "-":
                a = stack.pop()
                b = stack.pop()
                stack.push(int(b) - int(a))
            elif i == "+":
                a = stack.pop()
                b = stack.pop()
                stack.push(int(b) + int(a))
            elif i == "*":
                a = stack.pop()
                b = stack.pop()
                stack.push(int(b) * int(a))
            elif i == "**":
                a = stack.pop()
                b = stack.pop()
                stack.push(int(b)**int(a))
            elif i == "/":
                a = stack.pop()
                b = stack.pop()
                if int(a) == 0:
                    raise ValueError()
                stack.push(int(int(b) / int(a)))
            elif i == "<<":
                a = stack.pop()
                b = stack.pop()
                if (type(a) == type(3.5)) or (type(b) == type(3.5)):
                    raise PostfixFormatException("Illegal bit shift operand")
                stack.push(int(b) << int(a))
            elif i == ">>":
                a = stack.pop()
                b = stack.pop()
                if (type(a) == type(3.5)) or (type(b) == type(3.5)):
                    raise PostfixFormatException("Illegal bit shift operand")
                stack.push(int(b) >> int(a))
        else:  #Values
            try:
                stack.push(int(i))
            except ValueError:
                try:
                    stack.push(float(i))
                except ValueError:
                    raise PostfixFormatException("Invalid token")
    if (stack.size() > 1):
        raise PostfixFormatException("Too many operands")
    return stack.pop()
Пример #5
0
def prefix_to_postfix(input_str):
    input_str = rev_str(input_str)
    x = input_str.split(" ")
    stack = stack_array.Stack(len(x))
    ex = ""
    for i in x:  #Operators
        if is_operator(i):
            a = stack.pop()
            b = stack.pop()
            stack.push(a + " " + b + " " + i)
        else:  #Values
            stack.push(i)
    for i in range(stack.size()):
        ex += stack.pop() + " "
    exx = ex[0:-1]
    return exx
Пример #6
0
 def test_array_peek(self):
     stackT = stack_array.Stack(3)
     stackT.push(7)
     stackT.push(3)
     stackT.push(1)
     self.assertEqual(stackT.peek(), 1)
Пример #7
0
 def test_array_pop_0(self):
     stackT = stack_array.Stack(3)
     stackT.push(2)
     stackT.push(1)
     self.assertEqual(stackT.pop(), 1)
Пример #8
0
 def test_array_pop_1(self):
     stackT = stack_array.Stack(4)
     self.assertRaises(IndexError, lambda: stackT.pop())
Пример #9
0
 def test_array_push_1(self):
     stackT = stack_array.Stack(2)
     stackT.push(0)
     stackT.push(10)
     self.assertRaises(IndexError, lambda: stackT.push(1))
Пример #10
0
 def test_array_push_0(self):
     stackT = stack_array.Stack(4)
     stackT.push(3)
     stackT.push(2)
     stackT.push(1)
     self.assertEqual(stackT.items, [3, 2, 1, None])
Пример #11
0
 def test_array_is_full(self):
     stackT = stack_array.Stack(3)
     stackT.push(1)
     stackT.push(2)
     stackT.push(3)
     self.assertTrue(stackT.is_full())
Пример #12
0
 def test_array_is_empty(self):
     stackT = stack_array.Stack(7)
     self.assertTrue(stackT.is_empty())
Пример #13
0
 def test_simple_(self):
     stackT = stack_array.Stack(5)
     stackT.push(0)
     self.assertFalse(stackT.is_empty())
     self.assertFalse(stackT.is_full())
     self.assertEqual(stackT.size(), 1)