2020/18.py
def compute(tokens, i) -> Tuple[int, int]:
    """
    Parameters:
    tokens: is the list of tokens
    i: the index to start computing at

    Returns:
    A tuple of the computation result up to the next end parentheses, and the index at
    which we finished computing.
    """
    # This is a really dumb way of implicitly navigating the AST. These variables
    # basically keep track of the operation node.
    ismul = False
    isadd = False

    # This accumulates the computation at a level of the equation.
    x = 0

    while i < len(tokens):
        t = tokens[i]
        if isinstance(t, int) or t == "(":
            # If this is an open-parentheses, recursively call the compute function to
            # compute the value inside the parentheses.
            if t == "(":
                t, i = compute(tokens, i + 1)

            # Perform the necessary operation on x
            if ismul:
                # This is the case if we are in a situation like this:
                #    4 * 3
                #        ^
                #        i
                x *= t
                ismul = False
            elif isadd:
                x += t
                isadd = False
            else:
                x = t

        elif t == ")":
            return x, i
        elif t == "*":
            ismul = True
        elif t == "+":
            isadd = True

        i += 1

    return x, i

    Parameters:
    tokens: is the list of tokens
    i: the index to start computing at

    Returns:
    A tuple of the computation result up to the next end parentheses, and the index at
    which we finished computing.
    


A tuple of the computation result up to the next end parentheses, and the index at
which we finished computing.
A tuple of the computation result up to the next end parentheses and the index at which we finished computing .
def compute(tokens, i) -> Tuple[int, int]:
    ismul = False
    isadd = False
    x = 0
    while i < len(tokens):
        t = tokens[i]
        if isinstance(t, int) or t == "(":
            if t == "(":
                t, i = compute(tokens, i + 1)
            if ismul:
                x *= t
                ismul = False
            elif isadd:
                x += t
                isadd = False
            else:
                x = t
        elif t == ")":
            return x, i
        elif t == "*":
            ismul = True
        elif t == "+":
            isadd = True
        i += 1
    return x, i
def compute ( tokens , i ) -> Tuple [ int , int ] : ismul = False isadd = False x = 0 while i < len ( tokens ) : t = tokens [ i ] if isinstance ( t , int ) or t == "(" : if t == "(" : t , i = compute ( tokens , i + 1 ) if ismul : x *= t ismul = False elif isadd : x += t isadd = False else : x = t elif t == ")" : return x , i elif t == "*" : ismul = True elif t == "+" : isadd = True i += 1 return x , i
Parameters:
tokens: is the list of tokens
i: the index to start computing at
Parameters : tokens : is the list of tokens i : the index to start computing at
"""
    Parameters:
    tokens: is the list of tokens
    i: the index to start computing at

    Returns:
    A tuple of the computation result up to the next end parentheses, and the index at
    which we finished computing.
    """ # This is a really dumb way of implicitly navigating the AST. These variables # basically keep track of the operation node. # This accumulates the computation at a level of the equation. # If this is an open-parentheses, recursively call the compute function to # compute the value inside the parentheses. # Perform the necessary operation on x # This is the case if we are in a situation like this: #    4 * 3 #        ^ #        i
tokens
i