Reverse-Polish-Notation (RPN) is a machine friendly form of calculating expressions. Example, to evaluate, (1+2)*4 + 5 - 3 you enter the sequence into the calculator as, '5 1 2 + 4 * + 3 -' and obtain the result.

For this challenge write a RPN calculator for simple arithmetic expressions, '+','-','*','%','/'

Note: Chosen interpretations of operators, illustrated from first few test cases, are as follows: rpn('a','b','-') is interpreted as 'a-b', while rpn('a','b','/') is interpreted as 'b/a'. and rpz('a','b','%') which is interpreted as 'b%a'.

with operator '%' 'b % a' being mod(b,a) operator.

Following your 'stack' solution to http://www.mathworks.com/matlabcentral/cody/problems/1303-is-the-paranthesis-sequence-balanced try using a expression, and a value stack to work this problem.

4 Comments

Show
1 older comment

Alfonso Nieto-Castanon
on 1 Mar 2013

in your test suite it seems odd that rpz('a','b','-') is interpreted as 'a-b' while rpz('a','b','/') is interpreted as 'b/a' (instead of 'a/b'). Same goes for rpz('a','b','%') which is interpreted as 'b%a' instead of 'a%b'. Last, your last test case rpn(z(1),'+',z(2),'*',z(1),z(3),'-',z(4),z(5),'%','*') is plain weird... it should probably read rpn(z(1),z(2),'+',z(1),'*',z(3),'-',z(4),z(5),'%','*') instead...

Muthu Annamalai
on 2 Mar 2013

Thank you for your comments; I will update the description and fix last test case.

Muthu Annamalai
on 22 Mar 2013

I will retire the last test case for the problem.

Baki
on 6 Nov 2016 at 21:19

I have an rpn solver but it only accepts strings

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today