Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 prefix to infix rontoto Programmer named Tim Posts: 6 Threads: 3 Joined: Nov 2019 Reputation: 0 Likes received: 0 #1 Dec-03-2019, 11:05 AM 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 ()``` Gribouillis Posts: 2,509 Threads: 30 Joined: Jan 2018 Reputation: 240 Likes received: 583 #2 Dec-05-2019, 08:41 AM How would you determine by hand if parentheses are needed or not? Give a set of rules for this before writing the code. rontoto Programmer named Tim Posts: 6 Threads: 3 Joined: Nov 2019 Reputation: 0 Likes received: 0 #3 Dec-05-2019, 04:58 PM 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) Gribouillis Posts: 2,509 Threads: 30 Joined: Jan 2018 Reputation: 240 Likes received: 583 #4 Dec-05-2019, 05:38 PM (This post was last modified: Dec-05-2019, 05:38 PM by Gribouillis. Edited 2 times in total.) 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. rontoto Programmer named Tim Posts: 6 Threads: 3 Joined: Nov 2019 Reputation: 0 Likes received: 0 #5 Dec-08-2019, 12:32 PM (This post was last modified: Dec-08-2019, 12:32 PM by rontoto. Edited 1 time in total.) 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 ))``` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post String add prefix & suffix nahom 3 513 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 639 Jul-22-2019, 11:50 PM Last Post: Skaperen

Forum Jump:

Users browsing this thread: 1 Guest(s)