Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Happy 2006

by Roy Johnson (Monsignor)
on Jan 10, 2006 at 17:36 UTC ( [id://522254]=note: print w/replies, xml ) Need Help??


in reply to Happy 2006

An arguably cleverer way to write the nested loop without using some kind of module:
use strict; use warnings; my $year = @ARGV ? shift : 2006; my $ops = '{-,+,x,/,}'; my $globpat = '{-,}' . (join $ops, (reverse 1..9)); print $globpat, "\n"; for (glob $globpat) { tr/x/*/; print "$_ == $year\n" if $year == eval; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Happy 2006
by Perl Mouse (Chaplain) on Jan 11, 2006 at 01:27 UTC
    I'm not a big fan of the glob trick - there's always the possibility something matches (although for the given pattern, that's pretty unlikely). But just for kicks, I compared your solution with mine:
    $ /usr/bin/time ./perlmouse ; /usr/bin/time ./ray_johnson -9 + 8 * 7 + 654 * 3 - 2 - 1 == 2006 9 + 8 * 7 + 654 * 3 - 21 == 2006 9 + 8 * 7 * 6 * 5 - 4 + 321 == 2006 9 * 8 - 7 + 654 * 3 - 21 == 2006 56.60user 0.19system 2:04.01elapsed 39%CPU (0avgtext+0avgdata 0maxresi +dent)k 0inputs+0outputs (356major+87minor)pagefaults 0swaps -9+8*7+654*3-2-1 == 2006 9+8*7+654*3-21 == 2006 9+8*7*6*5-4+321 == 2006 9*8-7+654*3-21 == 2006 56.98user 3.45system 2:43.21elapsed 41%CPU (0avgtext+0avgdata 0maxresi +dent)k 0inputs+0outputs (398major+21556minor)pagefaults 0swaps
    The running time is about the same, but your solution uses a lot more memory, as it makes a list of all possible expressions to test before making the first test, while my solution tests each expressions right after it is constructed. My solution has a lot less pagefaults.
    Perl --((8:>*
      The running time is about the same, but your solution uses a lot more memory, as it makes a list of all possible expressions to test before making the first test, while my solution tests each expressions right after it is constructed. My solution has a lot less pagefaults.
      Ah, someday soon, glob will be an iterator in scalar context.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        It is, but that doesn't help here.
        As pointed out, glob is an iterator in scalar context. However, for provides list context.
        Perl --((8:>*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found