Discussion:
[Tutor] infix to postfix exponent handling
Quiles, Stephanie
2015-08-01 18:01:27 UTC
Permalink
My assignment calls for the program to be edited to handle the “^” symbol. the hint is that it should be done with just one line of code. Here is the assignment:
Modify the infix-to-postfix algorithm to handle exponentiation. Use the ^ symbol as the input token for testing.

Q-14: Modify the infixToPostfix function so that it can convert the following expression: 5 * 3 ^ (4 - 2)


Here is the code :

class Stack:
def __init__(self):
self.items = []

def isEmpty(self):
return self.items == []

def push(self, item):
self.items.insert(0,item)

def pop(self):
return self.items.pop(0)

def peek(self):
return self.items[0]

def size(self):
return len(self.items)


def infixToPostfix(infixexpr):
prec = {}
prec["^"] = 3
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()

for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)

while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)

print(infixToPostfix("5 * 3 ^ (4 - 2)"))
print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )”))

this is the lien that i added:

prec["^"] = 3

i also replaced the infixtopostfix to the problem:

("5 * 3 ^ (4 - 2)”))

here is the error I am getting :

Traceback (most recent call last):
File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 53, in <module>
print(infixToPostfix("5 * 3 ^ (4 - 2)"))
File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 45, in infixToPostfix
(prec[opStack.peek()] >= prec[token]):
KeyError: '(4'

Process finished with exit code 1

Please advise. not sure where i am failing with this

Thanks!!
Stephanie

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listi
Danny Yoo
2015-08-01 20:56:16 UTC
Permalink
Post by Quiles, Stephanie
("5 * 3 ^ (4 - 2)”))
File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 53, in <module>
print(infixToPostfix("5 * 3 ^ (4 - 2)"))
File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 45, in infixToPostfix
KeyError: '(4'
Ok. The immediate problem you are seeing here actually has nothing to do with
exponentiation, but with a lower-level issue.


Answering the following two questions should help you figure out
what's going on.

Given the string:

"5 * 3 ^ (4 - 2)"

1. What do you expect the list of tokens to be? Say concretely what
you expect it to be.

2. What does your program think the list of tokens is?
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/

Loading...