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 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_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())
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_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_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())
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 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
def test_stack_linked1(self): stack = StackLinked() self.assertEqual(stack.size(), 0) self.assertTrue(stack.is_empty())
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())