On 12/9/2019 6:21 AM, jezka...@gmail.com wrote:
Hi, I have got a problem.
Is this homework?
I wrote a code for prefix to infix. It works, but I need improve it
so on input there will be only necessary parentheses.
Define 'necessary'; give multiple input/output examples.
Put them in a test function.
Here is the code:
import re
a = input()
class Calculator:
def __init__ (self):
self.stack = []
def push (self, p):
if p in ['+', '-', '*', '/', "**"]:
op1 = self.stack.pop ()
op2 = self.stack.pop ()
self.stack.append ('(%s%s%s)' % (op1, p, op2))
print("op1 =", op1)
print("op2 =", op2)
print("p=", p)
print(self.stack)
else:
self.stack.append (p)
def convert (self, l):
l.reverse ()
for e in l:
self.push (e)
return self.stack.pop ()
c = Calculator ()
print (c.convert (re.findall(r'\d+|\*\*|[-+*/]', a)))
input is like /-*+*++**85 27 39 87 65 65 37 63 91
Since '*' and '**' are both allowed operators (see 'p in' above), spaces
are needed between operators to disambiguate. In any case, what output
does your code produce now, and what do you want it to produce.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list