Пример #1
0
def rpn(instr):
    stack = []
    for token in instr.split():
        if set(token).issubset("1234567890L"):
            stack.append(int(token.rstrip('L')))

        elif len(token) > 1 and token[0] == '-' \
                and set(token[1:]).issubset("1234567890L"):
            stack.append(int(token))

        # binary operators
        elif token in ('+', '-', '*', '/', '%', '**', 'x', 'xx'):
            b = stack.pop()
            a = stack.pop()
            if token == '+':
                res = a + b
            elif token == '-':
                res = a - b
            elif token == '*':
                res = a * b
            elif token == 'x':
                res = a * b
            elif token == '/':
                res = a / b
            elif token == '%':
                res = a % b
            elif token == '**':
                res = a**b
            elif token == 'xx':
                res = a**b
            stack.append(res)

        # unary operators
        elif token in ('!', '#', 'p!'):
            a = stack.pop()
            if token == '!':
                res = _primefac.listprod(xrange(1, a + 1))
            elif token == '#':
                res = _primefac.listprod(_primefac.primes(a + 1))
            elif token == 'p!':
                res = _primefac.listprod(_primefac.primes(a + 1))
            stack.append(res)
        else:
            raise Exception("Failed to evaluate RPN expression: not sure what "
                            "to do with '{t}'.".format(t=token))
    return [_primefac.mpz(i) for i in stack]
Пример #2
0
def rpn(instr):
    """RPN implementation
    """
    stack = []
    for token in instr.split():
        if set(token).issubset("1234567890L"):
            stack.append(int(token.rstrip("L")))
        elif (len(token) > 1 and token[0] == "-"
              and set(token[1:]).issubset("1234567890L")):
            stack.append(int(token))
        elif token in ("+", "-", "*", "/", "%", "**", "x",
                       "xx"):  # binary operators
            b = stack.pop()
            a = stack.pop()
            if token == "+":
                res = a + b
            elif token == "-":
                res = a - b
            elif token == "*":
                res = a * b
            elif token == "x":
                res = a * b
            elif token == "/":
                res = a / b
            elif token == "%":
                res = a % b
            elif token == "**":
                res = a**b
            elif token == "xx":
                res = a**b
            stack.append(res)
        elif token in ("!", "#", "p!"):  # unary operators
            a = stack.pop()
            if token == "!":
                res = listprod(range(1, a + 1))
            elif token == "#":
                res = listprod(primes(a + 1))
            elif token == "p!":
                res = listprod(primes(a + 1))
            stack.append(res)
        else:
            raise Exception(
                "Failed to evaluate RPN expression: not sure what to do with '{t}'."
                .format(t=token))
    return [_primefac.mpz(i) for i in stack]