Question
The AdvancedCalculator class represents many rows of calculators that support multiple expressions, as well as the use of variables. Lines will be separated by semicolons
The AdvancedCalculator class represents many rows of calculators that support multiple expressions, as well as the use of variables. Lines will be separated by semicolons (;) and each token will be separated by a single space. Each line starts with the variable name, then the "=" character, and then a mathematical expression. The last line will also require a mathematical expression to be returned. You can assume that the expression will not refer to variables that have not yet been defined. You can assume that the variable names will be consistent and case sensitive. A valid variable name is a non-empty string of alphanumeric characters, the first of which must be a letter. You must use a "calculator" to evaluate each expression in this class, otherwise, no credit will be given.
class AdvancedCalculator: ''' >>> C = AdvancedCalculator() >>> C.states == {} True >>> C.setExpression('a = 5;b = 7 + a;a = 7;c = a + b;c = a * 0;return c') >>> C.calculateExpressions() == {'a = 5': {'a': 5.0}, 'b = 7 + a': {'a': 5.0, 'b': 12.0}, 'a = 7': {'a': 7.0, 'b': 12.0}, 'c = a + b': {'a': 7.0, 'b': 12.0, 'c': 19.0}, 'c = a * 0': {'a': 7.0, 'b': 12.0, 'c': 0.0}, '_return_': 0.0} True >>> C.states == {'a': 7.0, 'b': 12.0, 'c': 0.0} True >>> C.setExpression('x1 = 5;x2 = 7 * ( x1 - 1 );x1 = x2 - x1;return x2 + x1 ^ 3') >>> C.states == {} True >>> C.calculateExpressions() == {'x1 = 5': {'x1': 5.0}, 'x2 = 7 * ( x1 - 1 )': {'x1': 5.0, 'x2': 28.0}, 'x1 = x2 - x1': {'x1': 23.0, 'x2': 28.0}, '_return_': 12195.0} True >>> print(C.calculateExpressions()) {'x1 = 5': {'x1': 5.0}, 'x2 = 7 * ( x1 - 1 )': {'x1': 5.0, 'x2': 28.0}, 'x1 = x2 - x1': {'x1': 23.0, 'x2': 28.0}, '_return_': 12195.0} >>> C.states == {'x1': 23.0, 'x2': 28.0} True >>> C.setExpression('x1 = 5 * 5 + 97;x2 = 7 * ( x1 / 2 );x1 = x2 * 7 / x1;return x1 * ( x2 - 5 )') >>> C.calculateExpressions() == {'x1 = 5 * 5 + 97': {'x1': 122.0}, 'x2 = 7 * ( x1 / 2 )': {'x1': 122.0, 'x2': 427.0}, 'x1 = x2 * 7 / x1': {'x1': 24.5, 'x2': 427.0}, '_return_': 10339.0} True >>> C.states == {'x1': 24.5, 'x2': 427.0} True >>> C.setExpression('A = 1;B = A + 9;C = A + B;A = 20;D = A + B + C;return D - A') >>> C.calculateExpressions() == {'A = 1': {'A': 1.0}, 'B = A + 9': {'A': 1.0, 'B': 10.0}, 'C = A + B': {'A': 1.0, 'B': 10.0, 'C': 11.0}, 'A = 20': {'A': 20.0, 'B': 10.0, 'C': 11.0}, 'D = A + B + C': {'A': 20.0, 'B': 10.0, 'C': 11.0, 'D': 41.0}, '_return_': 21.0} True >>> C.states == {'A': 20.0, 'B': 10.0, 'C': 11.0, 'D': 41.0} True >>> C.setExpression('A = 1;B = A + 9;2C = A + B;A = 20;D = A + B + C;return D + A') >>> C.calculateExpressions() is None True >>> C.states == {} True ''' def __init__(self): self.expressions = '' self.states = {}
def setExpression(self, expression): self.expressions = expression self.states = {}
def _isVariable(self, word): ''' >>> C = AdvancedCalculator() >>> C._isVariable('volume') True >>> C._isVariable('4volume') False >>> C._isVariable('volume2') True >>> C._isVariable('vol%2') False ''' # YOUR CODE STARTS HERE pass
def _replaceVariables(self, expr): ''' >>> C = AdvancedCalculator() >>> C.states = {'x1': 23.0, 'x2': 28.0} >>> C._replaceVariables('1') '1' >>> C._replaceVariables('105 + x') >>> C._replaceVariables('7 * ( x1 - 1 )') '7 * ( 23.0 - 1 )' >>> C._replaceVariables('x2 - x1') '28.0 - 23.0' ''' # YOUR CODE STARTS HERE pass
def calculateExpressions(self): self.states = {} calcObj = Calculator() # method must use calcObj to compute each expression # YOUR CODE STARTS HERE pass
pls pls pls do it in your own way
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