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 | |
Re^2: One Zero variants_without_repetition
by thenetfreaker (Friar) on Aug 08, 2007 at 06:13 UTC |
In Section
Seekers of Perl Wisdom