Cody

# Problem 1307. RPN Calculator for simple arithmetic expressions

Solution 726650

Submitted on 1 Sep 2015 by LY Cao
• Size: 68
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
z = rand(2,1)*10; val = rpn(z(1),z(2),'/') assert( val == z(2)/z(1) ) z = rand(2,1)*10; val = rpn(z(1),z(2),'%') assert( val == mod(z(2),z(1)) ) z = rand(2,1)*10; val = rpn(z(1),z(2),'/') assert( val == z(2)/z(1) ) z = rand(2,1)*10; val = rpn(z(1),z(2),'-') assert( val == (z(1)-z(2)) ) z = rand(2,1)*10; val = rpn(z(1),z(2),'+') assert( val == (z(1)+z(2)) ) z = rand(2,1)*10; val = rpn(3,4,'-',5,'+') assert( val == eval('3 - 4 + 5') ) val = rpn(5,1,2,'+',4,'*','+',3,'-') assert( val == eval('5 + ((1+2)*4) - 3') ) val = rpn(55,11,22,'+',44,'*','+',33,'-') assert( val == eval('55 + ((11+22)*44) - 33') ) val = rpn(553,112,221,'+',440,'*','+',30,'-') assert( val == eval('553 + ((112+221)*440) - 30') ) val = rpn(553,112,221,'+',440,'*','+',30,'-') assert( val == eval('553 + ((112+221)*440) - 30') ) %z = ceil(rand(1,5)*50); %z = [1,2,3,4,5]; %val = rpn(z(1),'+',z(2),'*',z(1),z(3),'-',z(4),z(5),'%','*') %q = mod( z(5),z(4))*(((z(1)+z(2))*z(1))-z(3)) %assert( val == q )

val = 0.2173 val = 0.3822 val = 0.2242 val = -6.0632 val = 9.4178 val = 4 val = 14 val = 1474 val = 147043 val = 147043