2010-09-18 46 views
2

我正在尝试使用Math :: Combinatorics生成一个数组的独特排列。为什么Perl的Math :: Combinatorics抱怨“必须使用未传递给构造函数的'frequency'参数的next_permutation”?

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"; 
} 

但是这个代码给我下面的错误:由于CPAN page说,它可以使用next_string()来完成必须使用不会传递到构造器“频率”的说法next_permutation,我不明白为什么。

回答

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

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

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