Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Happy 2006

by Limbic~Region (Chancellor)
on Jan 11, 2006 at 18:16 UTC ( [id://522521]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Happy 2006
in thread Happy 2006

Perl Mouse,
No, just each term in the expression is evaluated independently. This is how we did similar puzzles in school. The program works as follows:

Algorithm::Loops generates all the permutation of operators needed and then the numbers are zipped forming a single string. This is the point where eval $string would produce the same results as your code. Instead, I break each term apart and keep a running total of the value which is returned.

In my opinion, what would make the puzzle much more interesting would be to require single expression evaluation as your solution does but prohibit the use of eval $string. Here is my original solution modified accordingly though it is quite slow and a bit obfu now. Thanks for the puzzle.

#!/usr/bin/perl use strict; use warnings; use Algorithm::Loops qw{NestedLoops MapCar}; my $year = $ARGV[0] || 2006; my @digit = reverse 1..9; my $next = NestedLoops( [ ['+', '-'], ([qw{+ - / *}, '']) x $#digit ] +); while ( my @perm = $next->() ) { my @expr = split m|([/*+-])|, join '', MapCar { @_ } \@perm, \@dig +it; print "@expr\n" if evaluate(@expr) == $year; } sub evaluate { my @expr = @_; splice @expr, 0, 3, $expr[2] * ($expr[1] eq '-' ? -1 : 1); for (1 .. 2) { my $op = $_ % 2 ? qr|([*/])| : qr|([+-])|; for (my $i = 1; $i < $#expr;) { my ($x, $y) = ($expr[$i - 1], $expr[$i + 1]); if ($expr[$i] =~ /$op/) { my $val = $1 eq '*' ? $x * $y : $1 eq '/' ? $x / $y : +$1 eq '+' ? $x + $y : $x - $y; splice @expr, $i - 1, 3, $val; } else {$i += 2} } } return $expr[0]; }

Cheers - L~R

Replies are listed 'Best First'.
Re^4: Happy 2006
by Perl Mouse (Chaplain) on Jan 11, 2006 at 23:56 UTC
    In my opinion, what would make the puzzle much more interesting would be to require single expression evaluation as your solution does but prohibit the use of eval $string.
    You went to a school where you didn't have to program a calculator in your first year? I have a different opinion - writing an expression evaluator is boring, and it would be a reimplementation of functionality that perl already provides.

    I like string eval. I wish every programming language had such functionality.

    Perl --((8:>*
      Perl Mouse,
      You went to a school where you didn't have to program a calculator in your first year?

      No. Computers were pretty scarce in highschool and programming courses were non-existant.

      ...writing an expression evaluator is boring...

      I am sure doing anything you already know how to do can be boring. If you have had the benefit of a college education in computer science and/or work as a programmer professionally, building a precedence parser this trivial is probably extremely boring.

      I like string eval.

      I agree and use it when it makes sense to. OTOH, when people post neat puzzles like this I take it as an opportunity to learn things I don't already know. Since it isn't production code - there is no harm in a little golf, obfu, or non-straight forward solutions. I often intentionally limit the tools I will allow myself to use - see Necessity is the mother of invention for an explanation of why.

      Again, thanks for the puzzle.

      Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://522521]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found