diff options
Diffstat (limited to 'challenge-039/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-039/paulo-custodio/python/ch-2.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-039/paulo-custodio/python/ch-2.py b/challenge-039/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..50dd964346 --- /dev/null +++ b/challenge-039/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Challenge 039 +# +# TASK #2 +# Contributed by Andrezgz +# Write a script to demonstrate Reverse Polish notation(RPN). Checkout the wiki +# page for more information about RPN. + +import sys + +# simple rpn calculator +stack = [] + +def rpn(op): + if op.isdigit(): + stack.append(int(op)) + elif op == '+': + b = stack.pop() + a = stack.pop() + stack.append(a+b) + elif op == '-': + b = stack.pop() + a = stack.pop() + stack.append(a-b) + elif op == '*': + b = stack.pop() + a = stack.pop() + stack.append(a*b) + elif op == '/': + b = stack.pop() + a = stack.pop() + stack.append(a/b) + elif op == '.': + a = stack.pop() + print(a) + elif op.isspace(): + pass + else: + print("invalid operation:", op) + +for c in "".join(sys.argv[1:]): + rpn(c) |
