Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Golf: RPN calculator

by Sprad (Hermit)
on May 10, 2001 at 19:32 UTC ( #79436=perlmeditation: print w/replies, xml ) Need Help??

Passes -w, but not strict. No error checking, but it works for the 4 major functions. 102 strokes, not counting the shebang line.

Is there a better (shorter) way to do the $y=o;$x=o; $x$1$y part? You have to do it in that order or else division and subtraction give the wrong result, but it seems like that could be condensed further.

#!/usr/bin/perl -w sub p{push@s,shift}sub o{pop@s}for$n(@ARGV){if($n=~/(\D)/){$y=o;$x=o;p +(eval"$x$1$y")}else{p$n}}print o

A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Golf: RPN calculator
by japhy (Canon) on May 10, 2001 at 19:49 UTC
    I did this on Fun-With-Perl (FWP) back in late March.
    perl -le '{s!(\S+)\s+(\S+)\s*([-+*/])!"$1$3$2"!ee&&redo}print'

    japhy -- Perl and Regex Hacker
      I think you neglected to read any input... :) perl -ple's,(\S+)\s+(\S+)\s*([-+*/]),"$1$3$2",ee&&redo'


      I would try to explain, but I don't think I can.


Re: Golf: RPN calculator
by tadman (Prior) on May 10, 2001 at 22:50 UTC
    Interesting puzzle, and I'm not sure how you work japhy's solution, though it does seem interesting.

    Here's my take, which ended up being a variation on Sprad's even though it started out differently:
    sub p { # 68 characters my@s;for(@_){@s=(/^\D$/?(eval"$s[1]$_$s[0]",@s[2..$#s]):($_,@s))};@s } print p(@ARGV);
Re: Golf: RPN calculator
by jmcnamara (Monsignor) on May 11, 2001 at 12:17 UTC

    This was a great idea for a game of Golf. Recent outings have been too much like mini CS projects. Sorry Masem. ;-)

    This uses your methodology. There are 69 chars in the body of the program. It passes -w and strict. It also handle negative numbers:
    perl -le'/^\D$/?($_.=pop@_)&&push@_,eval pop(@_).$_:push@_,$_ for@ARGV +;print@_'

    I recently spent two months of my spare time implementing a type of RPN parser using Parse::RecDescent. Obviously I was taking the wrong approach. :-)


Re: Golf: RPN calculator
by danger (Priest) on May 10, 2001 at 19:56 UTC

    Here's one in 24 characters (not counting shebang):

    #!/usr/bin/perl -wl print eval"@ARGV[0,2,1]"

    Update: ARGG! I started along the lines of japhy's solution and suddenly, for no apparent reason, my brain skipped a neuron or ten and decided that one postfix op was the entire problem instead of an arbitrary rpn expression. Move along now ... no need to slow down and stare at the crash victim ...

      What if I feed it this:
      1 1 1 1 1 + + + +

      A fair fight is a sign of poor planning.

Re: Golf: RPN calculator
by jmcnamara (Monsignor) on Jul 26, 2001 at 17:00 UTC

    It is sad to say but I'm beginning to see everything through Golf tinted glasses. Even module releases.

    These are 45 chars. The arg list is included as an example:
    perl -MMath::RPN -le'print+rpn+join",",@ARGV' 100 9 '*' 5 / 32 + perl -MMath::RPN -le'$"=",";print+rpn"@ARGV"' 100 9 '*' 5 / 32 +


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://79436]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2017-05-27 14:47 GMT
Find Nodes?
    Voting Booth?