Hello, group!

I had a little free time today and was re-reading parts of "Perl Cookbook."
I came across section "4.19. Program: permute" and thought it would be
interesting to write my own version of permute before reading the Cookbook's
recipe. Of course, I had just read "4.16. Implementing a Circular List" and
thought it would be even more interesting to somehow incorporate a circular
list within the permute program.

This is what I came up with. Please let me know if the program is as
efficient as it might be paying special attention to the two lines with
questions in the comments.

Thanks,
ZO

----------BEGIN CODE----------
#!/usr/bin/perl
use warnings;
use strict;

my @data = 1 .. 9; # qw(man bites dog)
permute(\data);

sub permute {
  my $ref = ref $_[0] eq 'ARRAY' ? shift : [EMAIL PROTECTED];

  foreach my $i( 0 .. $#$ref ) {
    # Is this efficient?
    my @circular = @$ref[0 .. $i-1, $i+1 .. $#$ref];

    foreach ( 0 .. $#circular ) {
      print join(' ', $ref->[$i], @circular), "\n";
      # Is this efficient?
      push @circular, shift @circular;
    }
  }
}
-----------END CODE-----------



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to