From 21425cd7da79b2ea24c16958ce7a918ba4da69f2 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 28 Apr 2022 11:43:42 +0100 Subject: Add Python solution to challenge 039 --- challenge-039/paulo-custodio/python/ch-2.py | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-039/paulo-custodio/python/ch-2.py (limited to 'challenge-039/paulo-custodio/python/ch-2.py') 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) -- cgit