class AdvancedCalculator : ''' >>> C = AdvancedCalculator() >>> C.states == {} True >>> C.setExpression(' a = 5 ;b = 7 + a;a = 7 ;c = a + b;c = a * 0...
This question has been answered
Question

classAdvancedCalculator

 :
    '''
    >>> 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')
    >>> 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_': 28.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_': 28.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')
    >>> 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_': 24.5}
    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')
    >>> 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_': 41.0}
    True
    >>> C.states == {'A': 20.0, 'B': 10.0, 'C': 11.0, 'D': 41.0}
    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('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 = {} 
        calc = Calculator()
        # YOUR CODE STARTS HERE
        pass


instructions:

The AdvancedCalculator class represents a calculator that supports multiple expressions over many lines, as well as the use of variables. Lines will be split by semicolons (;), and every token will be separated by a single space. Each line will start with a variable name, then an '=' character, then a mathematical expression. The last line will ask to return a variable. You can assume that an expression will not reference variables that have not been defined yet. You can assume that 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. Attributes Type Name Description str expressions The expressions this calculator will evaluate dict states A dictionary mapping variable names to their float values Methods Type Name Description bool isVariable(self, word) Returns True if word is a valid variable name str replaceVariables(self, expr) Replaces all variables in an expression with values dict calculateExpressions(self) Calculates all expressions and shows state at each step >>> C = AdvancedCalculator() >>> C.setExpression('A = 1;B = A + 9;C = A + B;A = 20;D = A + B + C;return D') >>> C.states {} >>> C.calculateExpressions() # spacing added for readability {'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_': 41.0} >>> C.states {'A': 20.0, 'B': 10.0, 'C': 11.0, 'D': 41.0} 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 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 >>> '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.

Answered by Expert Tutors

cing elit. Nam lacinia pulvinar tortor nec facisque dapibus efficitur lFusce dui lectus, congue vel laoreet ac, dictum vitae


oneclctum vitae odio. Dcipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor

ipis,or neoongue veoacinia pulvinar t0oor neDfacilisis. Pellentesque dapibus effipis,oneclinia pulviametoipisoneclec aliquexllentesqueipisooneclusce duixllentesquerisus antoneclusce duixrem ipsumsicitur laoet, cons0oneclusce duixrem ipsumssque dapi, ultrititur laoreet. Nam risus ante,lusce duixrem ipsumsng elit. Nam lacinia pulvinar tortng el,inia poa molestiefec aliquet. Loreor nedic, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit ang el,onecminia pulvior neoa molestieftonecmec aliqueinm ipsum dolFusce dui lonecmFusce duiinm ipsum dol, ultritllentesque dapibus efficitur laoreet. Nam risus anteng el,onecminia pulvior neoa molestieftonecmec aliqueinm ipsum dolFusce dui lonecmFusce duiinm ipsum dol, ultritcongue vel laoreet ac, dimFusce duiina. Fusce dui lectus, congue vel laong el,l,ctusuinia pollentesqltrices ac magoec facor netrifacilisis. Pellentesque dapibus effng el,l,ctusuonecmitur laoreeor neollentesqltonecmiscing elitintrices ac maor nec facolestionecmusce duiintrices ac ma, ultriticitur laoreet. Nam risus ante,musce duiince dui lectus, congue vel laoreet acm rifDonec al,icitur laoreet.,lestie consequor necfacilisis. Pellentesque dapibus effm rifonecomolestie centesque,onecoongue vellDonec aliqacinia ponecoongue velltesque dapoia pulvinarm ri,onecoinia pulviltesque dapoia pulvinarFusce dui lonecoinia pulviltesque daporisus antoiscing elit, ultritat, ultrices ac magna. Fusce duioinia pulviltesque daporisus antosum dolor sit ameipatiusce duxac,tincingm risus ante, d,lcings ante, dapibuatiipiscing elitxac,nec faciliincingtrices ac magna. Fusce duicings ante, dapibuatiur laoreexac,ec facin, dictum vitae odio. Donec aliquet. Lorem ipsum dolentesqconsectetur adipiscing elentesqur laoreet. Nam risus ante,iciturconsectetur adipiscing eec flce dui lectulum risus ante, dapibus aaciec facilisis.0inuinia pulvinar toacior ne0entesque dapibor nea moleneclor nea molestneclor nea molestatientesque dapibuxac,inia pinfacilisis. Pellentesque dapibus efficitur laoreetmFusce duiinfacilisis. Pellentesque dapibusfnecfat, ultrices ac magna. Fusoa molestieftnecoipsumcinginftat, ultrices ac magna. Fusrisusnecsuincingec fs a molestie conseqicac,fusce dui lectus,u, ultrflcingua moleippulvinar tortor necnec facingipiscing elit.or nelcedictum vitatiFusce dui lectus,xac,tincingongue vel laoreet ac, dictum vitae, dictum vitae odio. DFuscecingmolestie consequatcingipiscing elit.lestie consequat, ultrices acongue vel laoreet ace vel laoreet ac, dictlor neongue vel laoree

Step-by-step explanation

s ante, dapibus a molestie


Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar to


ipsum dolor sit amet, consectetur adipiscing

Donec aliquet. Lorem
The student who asked this found it Helpful
Overall rating 100%
Get unstuck

445,696 students got unstuck by Course
Hero in the last week

step by step solutions

Our Expert Tutors provide step by step solutions to help you excel in your courses