Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prefix to infix
#1
Hello, i have got a rpbolem with prefix to infix code.
I need to change this code, so it can work with input like this: +-4 2 3 or *4-2 3
and the output must be without useless parenthesis ............ 4-2+3 or 4*(2-3)
Can u help me please? Here is the code
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) )
        elif p == '!':
            op = self.stack.pop ()
            self.stack.append ('%s!' % (op) )
        elif p in ['sin', 'cos', 'tan']:
            op = self.stack.pop ()
            self.stack.append ('%s(%s)' % (p, op) )
        else:
            self.stack.append (p)

    def convert (self, l):
        l.reverse ()
        for e in l:
            self.push (e)
        return self.stack.pop ()

c = Calculator ()
Reply
#2
How would you determine by hand if parentheses are needed or not? Give a set of rules for this before writing the code.
Reply
#3
Like in normal math.. so for example output in normal math will be 7+7+7+7 .. This code makes outputs like (7+7)+7)+7)
Reply
#4
rontoto Wrote:Like in normal math..
This doesn't make a set of rules. I know that mathematically, the result of 7+7+7 is the same as ((7+7)+7) but for your program it is very different because if the program outputs the latter, you'll say that there are useless parentheses and you want to output the former. So you need to define precisely in which cases the program should output parentheses and in which cases it should not. That's what I mean by a set of rules. If you cannot describe it with words, you cannot describe it with code.
Reply
#5
Yeah. I understand. But u know, problem is, when u are unsure about prefix to infix in real life because I do that for the first time. I changed my code, but dont know, if I coded all of the options. And second problem is, that input comes like this

/-*+*++* *85 27 39 87 65 65 37 63 91
or this
+*-73+54 64 52/*97 22 36

and i cannot remember how to change the input in code (I did gaps for each symbol for testing)
a = list(map(str, input().split()))
class Calculator:

    def __init__ (self):
        self.stack = []

    def push (self, p):
        if p in ['+', '-', '*', '/', "**"]:
            op1 = self.stack.pop ()
            op2 = self.stack.pop ()

            if p in['+', '-',"**"]:
                self.stack.append ('%s%s%s' % (op1, p, op2))
            if p in ["*", "/", ]:
                if  "+" or "-" not in self.stack:
                    self.stack.append('(%s)%s%s' % (op1, p, op2))
                else:
                    self.stack.append('%s%s(%s)' % (op1, p, op2))

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  prefix ID Number with 0,00 make 3 digit. mg24 1 708 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  String add prefix & suffix nahom 3 3,110 Sep-30-2019, 11:09 PM
Last Post: Gribouillis
  Best way of taking a date prefix from a line and forming a file path from it? Skaperen 5 2,923 Jul-22-2019, 11:50 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020