Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Calculator: def _ _ init _ _ ( self ) : self. _ _ expr = None @property def getExpr ( self ) :

class Calculator:
def __init__(self):
self.__expr = None
@property
def getExpr(self):
return self.__expr
def setExpr(self, new_expr):
if isinstance(new_expr, str):
self.__expr=new_expr
else:
print('setExpr error: Invalid expression')
return None
def _isNumber(self, txt):
# YOUR CODE STARTS HERE
try:
float(txt)
return True
except ValueError:
return False
pass
def _getPostfix(self, txt):
# YOUR CODE STARTS HERE
postfixStack = Stack() # method must use postfixStack to compute the postfix expression
precedence ={'+': 1,'-': 1,'*': 2,'/': 2,'^': 3}
output =''
for char in txt.split():
if self._isNumber(char):
output += char +''
elif char in precedence.keys():
while not postfixStack.isEmpty() and precedence.get(postfixStack.peek(),0)>= precedence.get(char,0):
output += postfixStack.pop()+''
postfixStack.push(char)
elif char =='(':
postfixStack.push(char)
elif char ==')':
while not postfixStack.isEmpty() and postfixStack.peek()!='(':
output += postfixStack.pop()+''
postfixStack.pop() # Discard '('
else:
print("Invalid character in expression:", char)
return None
while not postfixStack.isEmpty():
output += postfixStack.pop()+''
return output.strip()
@property
def calculate(self):
if not isinstance(self.__expr,str) or len(self.__expr)<=0:
print("Argument error in calculate")
return None
calcStack = Stack() # method must use calcStack to compute the expression
# YOUR CODE STARTS HERE
postfix_expr = self._getPostfix(self.__expr)
if postfix_expr is None:
print("Invalid expression")
return None
for token in postfix_expr.split():
if self._isNumber(token):
calcStack.push(float(token))
else:
if calcStack.isEmpty() or calcStack.size()<2: # Check for missing operands
print("Invalid expression: missing operands")
return None
operand2= calcStack.pop()
operand1= calcStack.pop()
if token =='+':
calcStack.push(operand1+ operand2)
elif token =='-':
calcStack.push(operand1- operand2)
elif token =='*':
calcStack.push(operand1* operand2)
elif token =='/':
if operand2==0:
print("Division by zero error")
return None
calcStack.push(operand1/ operand2)
elif token =='^':
calcStack.push(operand1** operand2)
else:
print("Invalid operator:", token)
return None
if calcStack.isEmpty() or calcStack.size()!=1: # Check for missing operators
print("Invalid expression: missing operators")
return None
return calcStack.pop()
pass
4.0) Testing postfix doctest cases (0/0) Testing code....
>>> x=Calculator()
>>> x._getPostfix('2^4')'2.04.0^'
>>> x._getPostfix('2.1*5+3^2+1+4.45')'2.15.0*3.02.0^+1.0+4.45+'
>>> x._getPostfix ('2.1*5+3^2+1+4.45','2.6786({5+3}^2+[1+4])')'2.67865.03.0+2.0^1.04.0++*'
Expected output: 2.04.0^
Your code returned: 24^
>>> Incorrect answer Expected output: 2.15.0*3.02.0^+1.0+4.45+
Your code returned: 2.15*32^+1+4.45+
>>> Incorrect answer Expected output: 2.67865.03.0+2.0^1.04.0++* Invalid character in expression: { Your code returned: None NOTE: Method is not returning a string Test Failed: True != False
5.0) Testing calculate doctest cases (0/0) Testing code....
>>> x=Calculator()>>> x.setExpr('2-3*4')
>>> x.calculate -10.0
>>> x.setExpr('2.5+3*[2+(3.0)(5^2-2*3^(2))*(4)]*(2/8+2{3-1/3})-2/3^2')
>>> x.calculate 1442.7777777777778 Expected output: -10.0 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

What is meant by staff authority ?

Answered: 1 week ago

Question

Discuss the various types of policies ?

Answered: 1 week ago

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago

Question

8. Demonstrate aspects of assessing group performance

Answered: 1 week ago