Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Section 3: The AdvancedCalculator class _isVariable(self, word) (5 points) Determines if the input is a valid variable name (see above for rules for names). The

Section 3: The AdvancedCalculator class

_isVariable(self, word) (5 points) Determines if the input is a valid variable name (see above for rules for names). The string methods str.isalpha()and str.isalnum()could be helpful here.

Input (excluding self)

str

word

The string to check if it is a valid variable name

Output

bool

True if word is a variable name, False otherwise

_replaceVariables(self, expr) (10 points) Replaces all variables in the input expression with the current value of those variables saved in self.states.

Input (excluding self)

str

expr

The input expression that may contain variables

Output

str

The expression with all variables replaced with their values

None

Nothing is returned if expression has invalid variables or uses a variable that has not been defined

calculateExpressions(self) (15 points)

Evaluates each expression saved in self.expressions. For each line, replace all variables in the expression with their values, then use a Calculator object to evaluate the expression and update self.states. This method returns a dictionary that shows the progression of the calculations, with the key being the line evaluated and the value being the current state of self.states after that line is evaluated. The dictionary must include a key named '_return_' with the return value as its value.

Hint: the str.split(sep)method can be helpful for separating lines, as well as separating the variable from the expression (for the lines that are formatted as var = expr). The str.strip()method removes the white space before and after a string. Don't forget dictionaries are mutable objects!

>>> 'hi;there'.split(';') ['hi', 'there']

>>> 'hi=there'.split('=')

['hi', 'there']

>>> ' hi there '.strip()

' hi there'

Output

dict

The different states of self.states as the calculation progresses.

None

Nothing is returned if there was an error replacing variables

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

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Describe the error. Sin / cos(- ) = sin / - cos = - tan

Answered: 1 week ago