aboutsummaryrefslogtreecommitdiff
path: root/challenge-039/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-28 11:43:42 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-28 11:43:42 +0100
commit21425cd7da79b2ea24c16958ce7a918ba4da69f2 (patch)
treebe5c72a613510817af8fbaf1edd9f36b9780e6d1 /challenge-039/paulo-custodio/python/ch-2.py
parentc6bbd320bc50a97da17f80a9b4e3cf26590a3574 (diff)
downloadperlweeklychallenge-club-21425cd7da79b2ea24c16958ce7a918ba4da69f2.tar.gz
perlweeklychallenge-club-21425cd7da79b2ea24c16958ce7a918ba4da69f2.tar.bz2
perlweeklychallenge-club-21425cd7da79b2ea24c16958ce7a918ba4da69f2.zip
Add Python solution to challenge 039
Diffstat (limited to 'challenge-039/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-039/paulo-custodio/python/ch-2.py43
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)