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)
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()
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)
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 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)
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)
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)
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())