Polish Notation

Parse a string containing a calculation in polish notation (aka Prefix notation). Print the result.

Spaces are used to separate operands. Only operators required are;
Add +
Subtract -
Multiply *

For negative numbers, there is no space between the minus sign and the number. For the minus operator there is a space before the first operand.

IN

- + 5 6 10

OUT

1

IN

+ - * 1 2 3 4

OUT

3

IN

- * 20 9 * 19 9

OUT

9

IN

- - - + -2 -3 -4 -5 -6

OUT

10
Login to submit solution


12 symbols pyth
Tue Sep 27 10:53:06 2016 Sait2000
.v:z"-\\b"\_
14 symbols pyth
Tue Sep 27 10:48:04 2016 Sait2000
.v:z"-(?! )"\_
15 symbols pyth
Mon Sep 19 15:17:26 2016 Sait2000
.v:z"-(?=\d)"\_
16 symbols pyth
Mon Sep 19 15:16:26 2016 Sait2000
.v:z"-(\d)""_\\1
17 symbols pyth
Mon Sep 19 15:16:04 2016 Sait2000
.v:z"-(\d)""_\\1"
28 symbols bash
Tue Aug 16 22:44:05 2016 hidefromkgb
sed '/-4/c10
/9/c9
/*/c3
c1'
38 symbols bash
Tue Aug 16 21:56:23 2016 hidefromkgb
sed '/^- +/c1
/^- \*/c9
/^+/c3
/-/c10'
40 symbols perl
Mon Jul 25 21:15:28 2016 k-mx
#!/usr/bin/perl -p
1while s/(.)( -?\d+)((?2))/"$2$1$3"/ee
41 symbols perl
Mon Jul 25 21:11:14 2016 k-mx
#!/usr/bin/perl -p
1while s/(.)( -?\d+)((?2))/"$2$1 $3"/ee
42 symbols bash
Mon Jul 25 08:40:24 2016 hidefromkgb
sed $'/^- +/c1\n/^- \*/c9\n/^+/c3\n/-/c10'
46 symbols perl
Fri Sep 26 16:36:47 2014 vakorol
#!/usr/bin/perl -p
1while s/(.) (-?\d+) (-?\d+)/eval"$2$1 $3"/e
48 symbols perl
Mon Aug 25 08:44:16 2014 gabrielmad
#!/usr/bin/perl -p
s/(.) (-?\d+) (-?\d+)/eval"$2$1 $3"/e while/ /
52 symbols perl
Thu Jun 19 13:19:07 2014 avkhozov
#!/usr/bin/perl -p
s/([+*-]) (-?\d+) (-?\d+)/eval"$2$1 $3"/e while/ /
56 symbols perl
Thu Jun 19 11:59:30 2014 avkhozov
#!/usr/bin/perl -p
s/([+*-]) (-?\d+) (-?\d+)/eval"$2$1 $3"/e while/[+*-]/
60 symbols perl
Thu Jun 19 11:50:20 2014 avkhozov
#!/usr/bin/perl -p
s/([+*-])\s(\-?\d+)\s(\-?\d+)/eval"$2$1 $3"/e while/[+*-]/
70 symbols bash
Mon Jul 25 08:26:20 2016 hidefromkgb
e=" \(-*[0-9(][^ ]*\)";echo $[`sed ":l;s/\([-+*]\)$e$e/(\2\1\3)/;tl"`]
141 symbols haskell
Fri Mar 13 18:13:00 2015 YoshikuniJujo
main=interact$show.p[].reverse.words
p(x:_)[]=x
p(x:y:s)([c]:o)|elem c"+-*"=p(t c x y:s)o
p s(d:o)=p(read d:s)o
t '+'=(+)
t '-'=(-)
t '*'=(*)
169 symbols perl
Sat Jun 14 02:08:10 2014 danieljabailey
#!/usr/bin/perl -na
for(@F){push@G,$_;while(@G[-1]!~/[\+\-\*]$/&&@G[-2]!~/[\+\-\*]$/&&@G>1){$a=pop@G;$b=pop@G;$o=pop@G;$r=$o eq'+'?($a+$b):$o eq'-'?($b-$a):($a*$b);push@G,$r}}print@G[0];
170 symbols perl
Fri Jun 13 22:58:08 2014 danieljabailey
#!/usr/bin/perl -na
for(@F){push@G,$_;while(@G[-1]!~/[\+\-\*]$/&&@G[-2]!~/[\+\-\*]$/&&@G>1){$a=pop@G;$b=pop@G;$o=pop@G;$r=$o eq'+'?($a+$b):$o eq'-'?($b-$a):($a*$b);push@G,$r;}}print@G[0];
171 symbols perl
Fri Jun 13 22:48:56 2014 danieljabailey
#!/usr/bin/perl -na
for(@F){push@G,$_;while(@G[-1]!~/[\+\-\*]$/&&@G[-2]!~/[\+\-\*]$/&&@G>1){$a=pop@G;$b=pop@G;$o=pop@G;$r=$o eq'+'?($a+$b):$o eq'-'?($b-$a):($a*$b);push@G,$r;}}print @G[0];

View all solutions