Exemplo n.º 1
0
def solve_expression(tokens):
    FixTokens(tokens)
    general_sum = 0
    curr_variable = 0
    curr_token_index = 0
    while curr_token_index < (len(tokens) - 1):
        current_token = tokens[curr_token_index]
        next_token = tokens[curr_token_index + 1]
        if next_token == "(":
            if is_number(current_token):
                raise UnapplicableOperator(current_token, curr_token_index)
            closing_bracket_ind = get_enclosing_bracket_ind(
                tokens[curr_token_index:], curr_token_index)
            if (closing_bracket_ind != False):
                value_in_brackets = solve_expression(
                    tokens[curr_token_index + 2:closing_bracket_ind])
                next_token = value_in_brackets
                curr_token_index = closing_bracket_ind
            else:
                raise LackOfBrackets(curr_token_index)

        general_sum, curr_variable = perform_current_operation(
            current_token, next_token, general_sum, curr_variable,
            curr_token_index)
        curr_token_index += 1
    general_sum += curr_variable
    return float(general_sum)
 def test4(self):
     s = "(33(11)22)"
     opening_ind = 15
     expected = 24
     self.assertEqual(expected, get_enclosing_bracket_ind(s, opening_ind))
 def test1(self):
     s = "( )"
     opening_ind = 15
     expected = 17
     self.assertEqual(expected, get_enclosing_bracket_ind(s, opening_ind))
 def test2(self):
     s = "("
     opening_ind = 15
     expected = False
     self.assertEqual(expected, get_enclosing_bracket_ind(s, opening_ind))