http://qs1969.pair.com?node_id=631126

in reply to One Zero variants_without_repetition

How about using a string and shuffle the ones from right to left using substr.

```use strict;
use warnings;

my \$raCombinations = combinary(4, 7);
print qq{\$_\n} for @\$raCombinations;

sub combinary
{
my (\$numZeros, \$numOnes) = @_;
my \$str = q{0} x \$numZeros . q{1} x \$numOnes;
my @combinations = (\$str);

my \$leftPtr = 0;
for my \$thisOne ( 1 .. \$numOnes )
{
for (
my \$offset = \$numZeros + \$thisOne - 2;
\$offset >= \$leftPtr;
\$offset --
)
{
substr \$str, \$offset, 2, q{10};
push @combinations, \$str;
}
\$leftPtr ++;
}

return \@combinations;
}

produces

```00001111111
00010111111
00100111111
01000111111
10000111111
10001011111
10010011111
10100011111
11000011111
11000101111
11001001111
11010001111
11100001111
11100010111
11100100111
11101000111
11110000111
11110001011
11110010011
11110100011
11111000011
11111000101
11111001001
11111010001
11111100001
11111100010
11111100100
11111101000
11111110000

I don't know whether this approach will be slower or faster than other suggestions. It's just the first idea that occurred to me.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: One Zero variants_without_repetition
by graff (Chancellor) on Aug 08, 2007 at 04:07 UTC
I gather that the OP also wants strings like:
```00011011111
00011101111
00011110111
00011111011
00011111101
00011111110
00110011111
00111001111
...
which your code didn't get around to. (And apparently, expecting to hold all results in memory before outputting them may be unrealistic.)
Re^2: One Zero variants_without_repetition
by thenetfreaker (Friar) on Aug 08, 2007 at 06:13 UTC
You are right about these strings that it doesn't get, as well as:
```.
.
.
01001011111
.
.
.
11011001011
.
.
etc