Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class Calculator: def _ _ init _ _ ( self ) : self. _ _ expr = None @property def getExpr ( self ) :
class Calculator:
def initself:
self.expr None
@property
def getExprself:
return self.expr
def setExprself newexpr:
if isinstancenewexpr, str:
self.exprnewexpr
else:
printsetExpr error: Invalid expression'
return None
def isNumberself txt:
# YOUR CODE STARTS HERE
try:
floattxt
return True
except ValueError:
return False
pass
def getPostfixself txt:
# YOUR CODE STARTS HERE
postfixStack Stack # method must use postfixStack to compute the postfix expression
precedence : : : : :
output
for char in txtsplit:
if self.isNumberchar:
output char
elif char in precedence.keys:
while not postfixStack.isEmpty and precedence.getpostfixStackpeek precedence.getchar:
output postfixStack.pop
postfixStack.pushchar
elif char :
postfixStack.pushchar
elif char :
while not postfixStack.isEmpty and postfixStack.peek:
output postfixStack.pop
postfixStack.pop # Discard
else:
printInvalid character in expression:", char
return None
while not postfixStack.isEmpty:
output postfixStack.pop
return output.strip
@property
def calculateself:
if not isinstanceselfexpr,str or lenselfexpr:
printArgument error in calculate"
return None
calcStack Stack # method must use calcStack to compute the expression
# YOUR CODE STARTS HERE
postfixexpr self.getPostfixselfexpr
if postfixexpr is None:
printInvalid expression"
return None
for token in postfixexpr.split:
if self.isNumbertoken:
calcStack.pushfloattoken
else:
if calcStack.isEmpty or calcStack.size: # Check for missing operands
printInvalid expression: missing operands"
return None
operand calcStack.pop
operand calcStack.pop
if token :
calcStack.pushoperand operand
elif token :
calcStack.pushoperand operand
elif token :
calcStack.pushoperand operand
elif token :
if operand:
printDivision by zero error"
return None
calcStack.pushoperand operand
elif token :
calcStack.pushoperand operand
else:
printInvalid operator:", token
return None
if calcStack.isEmpty or calcStack.size: # Check for missing operators
printInvalid expression: missing operators"
return None
return calcStack.pop
pass
Testing postfix doctest cases Testing code....
xCalculator
xgetPostfix
xgetPostfix
xgetPostfix
Expected output:
Your code returned:
Incorrect answer Expected output:
Your code returned:
Incorrect answer Expected output: Invalid character in expression: Your code returned: None NOTE: Method is not returning a string Test Failed: True False
Testing calculate doctest cases Testing code....
xCalculator xsetExpr
xcalculate
xsetExpr
xcalculate Expected output: Test Failed: 'Stack' object has no attribute 'size'
Debug the code pls
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started