On Wed, May 11, 2005 at 03:26:30PM -0400, Sean Quinlan wrote:
> On Wed, 2005-05-11 at 15:21 -0400, Bogart Salzberg wrote:
> > #!/usr/bin/perl
> > 
> > # by Bogart Salzberg, InkFist.Com
> > 
> > for (12345 .. 54321) {
> >     print ++$count, "\t", $_, "\n" if /
> >     ([1-5])
> >     (?!\1)([1-5])
> >     (?!\1|\2)([1-5])
> >     (?!\1|\2|\3)([1-5])
> >     (?!\1|\2|\3|\4)([1-5])
> >     /x;
> > }
> 
> Nice! Inspired this slightly simplified version:
> for (12345 .. 54321) {
>         print ++$count, "\t", $_, "\n"
>               if /[1-5]{5}/ && ! /([1-5]).*(\1)/;
> }

Answer 1:

Cute.  Another variation:

    grep { /1/ && /2/ && /3/ && /4/ && /5/ } 12345..54321;

However, going through and testing nearly 4000 numbers
to find the 120 that you want is rather expensive (suppose
the question was extended to all the 9-digit permutations
of 1..9).

Answer 2:

    sub print_perms {
        my $sofar = shift;
        my @used =  ( );
        unless(@_) {
            print $sofar;
            return;
        }
        while (@_) {
            my $next = shift;
            permcat( $sofar.$_, @used, @_ );
            push @used, $next;
        }
    }

    print_perms( "", 1..5 );

Answer 3:

The short answer is; read "Higher Order Perl" by Mark Jason
Dominus.  This is one of the simplest problems you'll see
solved in that book.  There are also many solutions of this
very problem.  It builds up to solutions that can be used for
many more purposes than just printing the list of results.
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to