Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's

by Tux (Canon)
on Sep 18, 2020 at 12:22 UTC ( [id://11121917]=note: print w/replies, xml ) Need Help??


in reply to Re: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
in thread Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's

This one ends up somewhere in the middle:

sub tux_for { my ($length, $ones) = @_; [ map { substr unpack ("b*", $_), 0, $length } grep { $ones == unpack "%32b*" => $_ } map { pack "L<", $_ } 0 .. oct "0b".join""=> (1) x $ones, (0) x ($length - $ones) ]; }

This one is 1200 x faster than Discipulus, but still 1200 x slower than the rest:

use Algorithm::FastPermute; sub tux_a_fp { my ($length, $ones) = @_; my @l = ((0) x ($length - $ones), (1) x $ones); my %seen; permute { $seen{join "" => @l}++ } @l; [ keys %seen ]; }

Enjoy, Have FUN! H.Merijn
  • Comment on Re^2: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found