2012-06-27 26 views
4
use Modern::Perl; 
use Algorithm::Permute; 
use List::AllUtils qw/uniq/; 

find_perms(1151); 

sub find_perms { 
    my ($value) = @_; 
    my @others; 
    my @digits = split(//, $value); 

    my $perm = Algorithm::Permute->new(\@digits); 

    while (my @next = $perm->next()) { 
    my $number = join('', @next); 
    push @others, $number; 
    } 
    @others = sort uniq @others; 

    # this one works correctly 
    # @others = sort (uniq(@others)); 

    say "FOUND @others"; 
} 

Output: 
FOUND 1115 1115 1115 1115 1115 1115 1151 1151 1151 1151 1151 1151 1511 1511 1511 1511 1511 1511 5111 5111 5111 5111 5111 5111 

嗨,Perl的算法:置換和列表:: AllUtils(uniq的)

發現指出,Algorithm::Permute是生產重複,很可能是由於「1在‘1151’的金額後,我決定使用uniq。但是,如果沒有括號使用sort uniq不會產生預期的結果。但sort(uniq(@x))呢。是什麼賦予了?

回答

6

perldoc -f sort列出了三個語法爲sort功能:

sort SUBNAME LIST 
sort BLOCK LIST 
sort LIST 

sort uniq @otherssort SUBNAME LIST語法之類的匹配。它預計uniq是比較全局變量$a$b並且返回<0,0>0以指示$a$b的相對排序的函數。

它看起來像你期待和希望的sort LIST語法,這就是你當你說的

sort(uniq(@others)) 
sort(uniq @others) 
4

之一,而@mob answered the question you asked,我想指出,我不知道,如果你想去sort uniq的方式。您仍然在存儲您不想存儲的元素。在這種情況下,最好使用散列。請致電when a piece of data is meant to be a string, use a string。從find_perms(0345)的輸出可能會讓你大吃一驚,但find_perms('0345')不會。

最後Algorithm::Permute::permute非常快。

考慮到這些因素,我將重新編寫代碼:

use strict; use warnings; 
use Algorithm::Permute qw(permute); 

my $uniq_perms = find_perms('115122345'); 

sub find_perms { 
    my ($value) = @_; 

    my @digits = split //, $value; 
    my %uniq; 

    permute { $uniq{join('', @digits)} = undef } @digits; 

    return [ keys %uniq ]; 
} 
+0

如果我能接受兩個答案我會的!我可能會這樣。謝謝。 – Richard

5

Algorithm::LoopsNextPermute不會產生重複,所以沒有必要花費內存和CPU擺脫他們。

use Algorithm::Loops qw(NextPermute); 

sub find_perms { 
    my @rv; 
    my @digits = sort split //, $_[0]; 
    do { 
     push @rv, join '', @digits; 
    } while NextPermute(@digits); 
    return @rv; 
} 

say for find_perms(1151); 

1115 
1151 
1511 
5111