MCPcopy Create free account
hub / github.com/geekcomputers/Python / infixToPostfix

Method infixToPostfix

Infix_to_Postfix.py:53–86  ·  view source on GitHub ↗
(self, exp)

Source from the content-addressed store, hash-verified

51 # The main function that converts given infix expression
52 # to postfix expression
53 def infixToPostfix(self, exp):
54 # Iterate over the expression for conversion
55 for i in exp:
56 # If the character is an operand,
57 # add it to output
58 if self.isOperand(i):
59 self.output.append(i)
60
61 # If the character is an '(', push it to stack
62 elif i == "(":
63 self.push(i)
64
65 # If the scanned character is an ')', pop and
66 # output from the stack until and '(' is found
67 elif i == ")":
68 while (not self.isEmpty()) and self.peek() != "(":
69 a = self.pop()
70 self.output.append(a)
71 if not self.isEmpty() and self.peek() != "(":
72 return -1
73 else:
74 self.pop()
75
76 # An operator is encountered
77 else:
78 while not self.isEmpty() and self.notGreater(i):
79 self.output.append(self.pop())
80 self.push(i)
81
82 # pop all the operator from the stack
83 while not self.isEmpty():
84 self.output.append(self.pop())
85
86 print("".join(self.output))
87
88
89# Driver program to test above function

Callers 1

Calls 7

isOperandMethod · 0.95
pushMethod · 0.95
isEmptyMethod · 0.95
peekMethod · 0.95
popMethod · 0.95
notGreaterMethod · 0.95
appendMethod · 0.45

Tested by

no test coverage detected