Reputation: 33
I am trying to simply add inputs to these codes but because of package pythonds
i keep getting this error.
ModuleNotFoundError: No module named 'pythonds'.
This error is keeping me from finishing and I am unsure how I can get past this error. Please help and thank you in advance.
from pythonds.basic.stack import Stack
def infixToPostfix(infixexpr):
prec = {}
prec["^"] = 4
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)
def postfixEval(postfixExpr):
operandStack = Stack()
tokenList = postfixExpr.split()
for token in tokenList:
if token in "0123456789":
operandStack.push(int(token))
else:
operand2 = operandStack.pop()
operand1 = operandStack.pop()
result = doMath(token,operand1,operand2)
operandStack.push(result)
return operandStack.pop()
def doMath(op, op1, op2):
if op == "*":
return op1 * op2
elif op == "/":
return op1 / op2
elif op == "+":
return op1 + op2
elif op == "^":
return op1 ** op2
elif op == "%":
return op1 % op2
else:
return op1 - op2
string = input("Enter a string: ")
print(infixToPostfix(string))
Upvotes: 2
Views: 4346
Reputation: 5569
Make sure to pip install pythonds
. I was able to use that import statement in the Python prompt without any issues:
>>> from pythonds.basic.stack import Stack
Upvotes: 7