为什么Perl的Math :: Combinatorics抱怨“必须使用'频率'参数的next_permutation而不传递给构造函数”?

时间:2010-09-18 10:36:04

标签: perl permutation

我正在尝试使用Math :: Combinatorics生成数组的唯一排列。正如CPAN page所说,可以使用next_string():

完成
use Math::Combinatorics;
my @arr = [1,1,1,0,0];  
$c = Math::Combinatorics->new( count=>5, data=>[\@arr], frequency=>[3,2] );
while (@permu = $c->next_string()){
print "@permu\n";
}  

但是这段代码给出了以下错误:必须使用未传递给构造函数的'frequency'参数的next_permutation,我无法理解为什么。

2 个答案:

答案 0 :(得分:6)

您在该计划中遇到很多问题。您的数据类型不匹配。

如果您想使用frequency,则只需指定一次唯一元素,但指定它们出现的次数。您为频率提供的数组引用必须与数据数组的长度相同:

use Math::Combinatorics;

my @array = (1,0); # an array, not an array reference

$c = Math::Combinatorics->new( 
    count     => 5,
    data      => \@array,        # now you take a reference    
    frequency => [3,2] 
    );

while (@permu = $c->next_string ){
    print "@permu\n";
    }

现在你应该得到你想要的输出,这是你不能分辨多个1和多个0的区别的不同组合:

0 1 1 1 0
0 1 1 0 1
0 1 0 1 1
0 0 1 1 1
1 0 1 1 0
1 0 1 0 1
1 0 0 1 1
1 1 0 1 0
1 1 0 0 1
1 1 1 0 0

如果您不使用frequency,则只需指定数据数组中的所有元素即可。但是,您可能正在避免这种情况,因为它将每个元素视为不同的元素,因此它不会崩溃看起来像是相同的组合。

答案 1 :(得分:1)

虽然我没有使用过这个包the documentation说频率必须与数据构造函数参数的长度相同。

在你的例子中,长度不一样,可能这就是问题所在。