Hello dear monks,
I'm trying to create a function that by getting a number of Ones and Zeroes (if possible also the number of the generation) shall give eventually give all the conbinations without repeats, because Algorithm::Combinatorics can't differ 2 zeroes( for it 010 and 010 is different, but for me it's the same).
I've come with an idea of printing them chronologically like (if $ones=2 and $zeroes=3):
00011
00101
01001
10001
00110
01010
10010
01100
10100
11000
where these 10 are all variations without repeats.
i tried achieving my goal in 2 ways :
1. trying to simply print then like that -
sub combinationN {
my ($O,$Z,$current) = @_;
my $string = '';
my $toMove = 0;
my @states;
my $now = $current - ($current-$Z);
$states[0] = $current;
while ($current > $Z) {
$current -= $now+1;
$states[$toMove] = $current % ($Z+1);
$toMove++;
}
for (1..$Z-$states[0]) {$string .= '0'}
for (1..$O-$toMove) {$string .= '1'}
# foreach (@states) {
for (1..$states[0]) {$string .= '0'}
for (1..$toMove) {$string .= '1'}
# }
print "$string\n";
return $string
}
but this gave out
00011
00110
01100
11000
00101
01001
10001
and afterwars there were strange thigs like 000110000, etc;
also it didn't print 00110,01010,...,01100,etc.
2. printing them by distanses from zero to one using a flipFlop flag (after a set of ones there must com a set of zeroes and the opposite) -
sub combinationN {
my ($O,$Z,$current,$tL) = @_;
my $string = '';
my $ff = 1;
my $Dc = 0;
my $Oc = int($current % $O); ##
my $Zc = int($current % $Z); ##
my $place = 1;
while ($tL > $place) {
if ($ff eq 1) {
$Dc = $O-$Oc;
for (1..$Dc) {$string .= '1'}
$Oc = $Dc;
} else {
$Dc = $Z-$Zc;
for (1..$Dc) {$string .= '0'}
$Zc = $Dc;
}
$place += $Dc;
$ff *= -1;
}
print "$string\n";
return $string
}
but this gave out
11000
1001
11000
1000
1100
10100
11000
1001
and many other strange reasults.
I searched Google and Google-Code-search and the only similar things i found were connected to colors and cropping the results that repeated (but still all the results( 24! ) were checked).
Is there a simple logical way to print it out( using map() or/and join()) or fixing one of the codes i tried ?
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.