in reply to Re^3: Reverse Polish Notation Question
in thread RPN Question

For a linear equation it could look like this (and yes, it took me more than 15 minutes...)

use strict; use warnings; my $equation = "(2*(4*x-8)+3*x)-(10*x+1-9)"; # " = 0" omitted $equation =~ s/x/\$_[0]/g; my $eval = sub { eval $equation }; my $val1 = $eval->(0); my $val2 = $eval->(1); die "No solution for this equation\n" unless $val1 - $val2; my $result = -$val1 / ($val2 - $val1); printf "Result is %.10f\n", $result;

Replies are listed 'Best First'.
Re^5: Reverse Polish Notation Question
by Laurent_R (Canon) on May 02, 2015 at 22:13 UTC
    I tried to time your version and mine (removing the printouts). As I would expect yours is faster, but mine is converging fast enough to make the difference very small:

    Your solution:

    $ time perl -e 'use strict; > use warnings; > my $val2 > = $eval->(1); my $equation = "(2*(4*x-8)+3*x)-(10*x+1-9)"; # " = 0" omitted > $equation =~ s/x/\$_[0]/g; > my $eval = sub { eval $equation }; > my $val1 = $eval->(0); > my $val2 = $eval->(1); > die "No solution for this equation\n" unless $val1 - $val2; > my $result = -$val1 / ($val2 - $val1); > > printf "Result is %.10f\n", $result;' Result is 8.0000000000 real 0m0.044s user 0m0.015s sys 0m0.031s
    and mine:
    $ time perl solver.pl Result is: 8.0000000000 real 0m0.050s user 0m0.031s sys 0m0.015s
    0.44 versus 0.50 sec. I might have been lucky on this one, but, frankly, my narrowing-down algorithm works fairly well.

    Je suis Charlie.
Re^5: Reverse Polish Notation Question
by Laurent_R (Canon) on May 02, 2015 at 17:44 UTC
    Hi hdb, that's a pretty neat solution.

    It took me twenty seconds or so to figure out why you were replacing x by $_[0]. I must say that this is a nice clever idea.

    Je suis Charlie.