Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by Discipulus (Abbot)
on Sep 18, 2020 at 08:35 UTC ( #11121897=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

UPDATE this code is wrong as noticed by choroba and hippo. See below why

Hello Eily,

I find even more intuitive to use permutations directly:

use strict; use warnings; use feature 'say'; use Algorithm::Combinatorics qw(permutations); my $zeros = 7; my $ones = 3; # if you want the iterator force scalar context, as in: # my $iter = permutations(\@data); say join('',@$_) for permutations( [ (0) x $zeros, (1) x $ones] );
L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re^2: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's -- permutations
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's -- permutations
by choroba (Archbishop) on Sep 18, 2020 at 09:12 UTC
    It returns far more results than needed, as they are repeated, because for permutations, zero at position 0 is different to zero at position 1.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's -- permutations
by hippo (Bishop) on Sep 18, 2020 at 09:21 UTC

    This is neat code but is doing far too much work because it permutes every zero with every other zero (and also every one with every other one). eg. if you run it with 2 zeros and 2 ones you will get 4 duplicates for each answer. Increasing the numbers just makes the problem exponentially worse. Try with 9 zeros and 1 one and you'll soon see what I mean :-)


    🦛

      yes thanks choroba and hippo, my code presented above is blatantly wrong :)

      Can I add a %seen to repair my sin?

      use strict; use warnings; use feature 'say'; use Algorithm::Combinatorics qw(permutations); my $zeros = 7; my $ones = 3; my %seen; # this is really ugly # say join '', @$_ for grep{ !$seen{join('', @$_)}++ } permutations( [ + (0) x $zeros, (1) x $ones] ); # a little nicer say for grep { !$seen{$_}++ } map{join '', @$_} permutations( [(0) x $ +zeros, (1) x $ones] );

      Now the cure is worst than the disease, but runs ok

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
by Eily (Monsignor) on Sep 18, 2020 at 08:39 UTC

    Yup. I guess it was just too obvious for me or something ^^".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-12-05 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?