MCPcopy Index your code
hub / github.com/ndleah/python-mini-project / convert_to_postfix

Function convert_to_postfix

infix_postfix_calculator/main.py:16–49  ·  view source on GitHub ↗
(infix)

Source from the content-addressed store, hash-verified

14
15
16def convert_to_postfix(infix):
17 ret = ""
18 infix = infix.strip()
19 infix_arr = infix.split(' ')
20 s = []
21 for token in infix_arr:
22 if token == "(":
23 s.append(token)
24 elif token == "+":
25 while s and s[-1] in ["+", "-", "*", "/"]:
26 ret += s.pop() + " "
27 s.append(token)
28 elif token == "-":
29 while s and s[-1] in ["+", "-", "*", "/"]:
30 ret += s.pop() + " "
31 s.append(token)
32 elif token == "*":
33 while s and s[-1] in ["*", "/"]:
34 ret += s.pop() + " "
35 s.append(token)
36 elif token == "/":
37 while s and s[-1] in ["*", "/"]:
38 ret += s.pop() + " "
39 s.append(token)
40 elif token == ")":
41 while s and s[-1] != "(":
42 ret += s.pop() + " "
43 if s and s[-1] == "(":
44 s.pop()
45 else:
46 ret += token + " "
47 while s:
48 ret += s.pop() + " "
49 return ret
50
51
52def calculate_postfix(postfix):

Callers 1

calculateFunction · 0.85

Calls 3

stripMethod · 0.80
splitMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected