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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Yours is also faster (albeit only 7% faster) than my grep approach, but my regexp approach is 13% faster than yours.

use strict; use warnings; use Algorithm::Loops qw( NestedLoops ); use Benchmark qw( cmpthese ); # Note: I improved the speed and the interface of ruzam's solution. sub _ruzam { my ($source, $hash) = @_; return if $hash->{$source}; my @parts = split(/:/, $source); return if @parts < 1; ++$hash->{$source}; for my $i (0..$#parts) { my @parts_copy = @parts; splice(@parts_copy, $i, 1); _ruzam(join(':', @parts_copy), $hash); } } sub ruzam { my ($source) = @_; my %hash; _ruzam($source, \%hash); return keys %hash; } sub ikegami1 { my ($source) = @_; my @parts = split(/:/, $source); return NestedLoops( [ [ 0..$#parts ], ( sub { [ $_+1..$#parts ] } ) x $#parts, ], { OnlyWhen => 1 }, sub { join(':', map $parts[$_], @_) }, ); } sub ikegami2 { my ($source) = @_; my @parts = split(/:/, $source); my @rv; for my $comb (1..2**@parts-1) { push @rv, join ':', map $parts[$_], grep $comb & (1<<$_), 0..$#parts; } return @rv; } sub ikegami3 { local $_ = ":$_[0]:"; my $parts = tr/:/:/ - 1; my $re = '(?{ "" })' . '(:[^:]*)(?=:)(?{ $^R . $^N })' . '(?:.*(:[^:]*)(?=:)(?{ $^R . $^N })' x ($parts-1) . ')?' x ($parts-1) . '(?{ push @rv, substr($^R, 1) })' . '(?!)'; { use re 'eval'; $re = qr/$re/; } local our @rv; /$re/; return @rv; } sub rhesa { my ($source) = @_; my @parts = split /:/, $source; my @rv; for my $i( 1 .. 2**@parts - 1 ) { my @ar; my $t = 0; while( $i > 0 ) { push @ar, $parts[$t] if $i & 1; $i >>= 1; $t++; } push @rv, join ':', @ar; } return @rv; } { local our $source = 'horse:cow:dog:cat'; my $expected = 'cat|cow|cow:cat|cow:dog|cow:dog:cat|dog|dog:cat|hor +se|horse:cat|horse:cow|horse:cow:cat|horse:cow:dog|horse:cow:dog:cat| +horse:dog|horse:dog:cat'; foreach (qw( ruzam ikegami1 ikegami2 ikegami3 rhesa )) { printf("%-9s ", "$_:"); my $rv = join '|', sort do { no strict 'refs'; \&{$_} }->($source); if ($rv eq $expected) { print("ok"); } else { print("bad ($rv)"); } print("\n"); } print("\n"); cmpthese(-3, { # ruzam => q{ use strict; use warnings; my @rv = ruzam our + $source; 1 }, # ikegami1 => q{ use strict; use warnings; my @rv = ikegami1 our + $source; 1 }, # ikegami2 => q{ use strict; use warnings; my @rv = ikegami2 our + $source; 1 }, # ikegami3 => q{ use strict; use warnings; my @rv = ikegami3 our + $source; 1 }, # rhesa => q{ use strict; use warnings; my @rv = rhesa our + $source; 1 }, ruzam => q{ my @rv = ruzam $source; 1 }, ikegami1 => q{ my @rv = ikegami1 $source; 1 }, ikegami2 => q{ my @rv = ikegami2 $source; 1 }, ikegami3 => q{ my @rv = ikegami3 $source; 1 }, rhesa => q{ my @rv = rhesa $source; 1 }, }); }
Rate ikegami1 ruzam ikegami2 rhesa ikegami3 ikegami1 2381/s -- -19% -66% -68% -72% ruzam 2944/s 24% -- -58% -61% -65% ikegami2 7072/s 197% 140% -- -5% -16% rhesa 7478/s 214% 154% 6% -- -11% ikegami3 8420/s 254% 186% 19% 13% --

In reply to Re^2: possible combinations in sequence by ikegami
in thread possible combinations in sequence by ruzam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (3)
    As of 2020-11-01 01:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (291 votes). Check out past polls.

      Notices?