Argh, i'm damn slow.. i was sure the answer was in the Tartaglia's triangle, but no way.
here my, plain as usual, solution:
use strict;
use warnings;
my @posts = (1..$ARGV[0] || 5);
my @pigeons = (1..$ARGV[1] || 3);
print +('1 ' x @pigeons)."\n" if @posts == @pigeons;
die sprintf "%s is not enought for %s pigeons!",scalar @posts,scalar
+@pigeons
if @pigeons > @posts;
for (@pigeons) {
my $max = @posts - @pigeons + 1;
while ($max > 1){
my @distr = (0) x ($#pigeons+1);
$distr[0] = $max;
my $remain = @posts - $max;
my $i=0;
while ($remain > 0){
$i == $#distr ? $i=1 : $i++;
$distr[$i]++;
$remain--;
}
$max--;
print +(map{ "$distr[$_-1] "} @pigeons),"\n";
}
@pigeons = (pop @pigeons,@pigeons);
}
__DATA__
perl BUk-pigeons.pl 5 3
3 1 1
2 2 1
1 3 1
1 2 2
1 1 3
2 1 2
thanks for the amusement!
At the end of writing i was surpised i needed not an hash to get uniques results..
L*
UPDATE: sadly my solutions gives too few combinations.. perl BUk-pigeons.pl 12 7 | wc -l give me only 35 combinations..
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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|