<commit_before>import re
from collections import deque

class Calculator:
    def calculate(self, input_string):
        elems = re.findall('\d+|[+-]', input_string)
        output = deque([])
        operators = []
        for e in elems:
            if e.isdigit():
                output.append(float(e))
            else:
                if len(operators) > 0:
                    output.append(operators.pop())
                operators.append(e)

        while len(operators) > 0:
            output.append(operators.pop())

        return self.parseRPN(output.popleft(), output.popleft(), output.popleft(), output)

    def parseRPN(self, op1, op2, op, rest):
        if op == "+": result = op1 + op2
        if op == "-": result = op1 - op2
        if len(rest) > 0:
            return self.parseRPN(result, rest.popleft(), rest.popleft(), rest)
        else:
            return result<commit_msg>refactor reverse polish notation eval<commit_after>import re
from collections import deque

class Calculator:
    def calculate(self, input_string):
        elems = re.findall('\d+|[+-]', input_string)
        output = deque([])
        operators = []
        for e in elems:
            if e.isdigit():
                output.append(float(e))
            else:
                self.__append_operator(operators, output, e)

        while len(operators) > 0:
            output.append(operators.pop())

        return self.__eval_rpn(output)

    def __eval_rpn(self, output):
        operands = []
        while len(output) > 0:
            token = output.popleft()
            if isinstance(token, float):
                operands.append(token)
            else:
                if token == "+": self.__add(operands)
                if token == "-": self.__subtract(operands)

        if len(operands) == 1:
            return operands.pop()
        else: raise RuntimeError("Input has too many values")

    def __append_operator(self, operators, output, elem):
        if len(operators) > 0:
            output.append(operators.pop())
        operators.append(elem)

    def __add(self, operands):
        if len(operands) < 2:
            raise RuntimeError("Addition requires 2 operands")
        else:
            result = operands.pop() + operands.pop()
            operands.append(result)

    def __subtract(self, operands):
        if len(operands) < 2:
            raise RuntimeError("Subtraction requires 2 operands")
        else:
            op2 = operands.pop()
            op1 = operands.pop()
            operands.append(op1 - op2)
{'fraction_non_alphanumeric': 0.11750881316098707, 'fraction_numerical': 0.008225616921269096, 'mean_word_length': 1.8504464285714286, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5164492', 'n_tokens_mistral': 761, 'n_tokens_neox': 730, 'n_words': 179}