Last active
November 13, 2016 22:56
-
-
Save u8y7541/c8c5817476742325bf085b2036b6e95b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stackAdd, stackSub, stackDiv, stackMult, stackExp, stackMod, stackFac, stackIntDiv, inner, evalRPN = lambda stack: stack.append(stack.pop() + stack.pop()), lambda stack: stack.append((stack.pop() - stack.pop()) * -1), \ | |
lambda stack: stack.append(1 / (stack.pop() / stack.pop())), lambda stack: stack.append(stack.pop() * stack.pop()), \ | |
lambda stack: stack.append(stack[-2] ** (stack.pop(), stack.pop())[0]), lambda stack: \ | |
stack.append(stack[-2] % (stack.pop(), stack.pop())[0]), \ | |
lambda stack: stack.append(eval(''.join([str(i) + '*' for i in range(1, int(stack.pop()) + 1)]) + '1')), \ | |
lambda stack: stack.append((lambda a, b: b // a)(stack.pop(), stack.pop())), \ | |
lambda r, operators, stack = []: \ | |
([stack.append(float(i)) if i not in [j[0] for j in operators] else \ | |
[exec('stack' + j[1:] + '(stack)', globals(), {'stack':stack}) \ | |
for j in operators if i == j[0]] for i in r.split(' ')], stack[-1])[1], \ | |
lambda r, operators = ['+Add', '-Sub', '/Div', '&IntDiv', '*Mult', '^Exp', '%Mod', '!Fac']: inner(r.replace('//', '&'), operators) | |
print(evalRPN('0.5 1 2 ! * 2 1 ^ + 10 + *')) | |
print(evalRPN('1 2 3 4 ! + - / 100 *')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment