Perl算法:排列和列表::AllUtils(uniq)

Perl算法:排列和列表::AllUtils(uniq),perl,Perl,嗨 在发现它正在生成重复项后,很可能是由于“1151”中的“1”数量,我决定使用uniq。但是,使用不带括号的排序uniq,不会产生预期的结果。但是排序(uniq(@x))可以。给出了什么?列出了排序功能的三种语法: use Modern::Perl; use Algorithm::Permute; use List::AllUtils qw/uniq/; find_perms(1151); sub find_perms { my ($value) = @_; my @others

在发现它正在生成重复项后,很可能是由于“1151”中的“1”数量,我决定使用
uniq
。但是,使用不带括号的
排序uniq
,不会产生预期的结果。但是排序(uniq(@x))可以。给出了什么?

列出了
排序功能的三种语法:

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
sort uniq@others
与排序的
sort子名称列表
语法匹配。它期望
uniq
是一个比较全局变量
$a
$b
的函数,并返回
0
以指示
$a
$b
的相对顺序

看起来您期待并想要
排序列表
语法,当您说出其中一个

sort SUBNAME LIST
sort BLOCK LIST
sort LIST
然而,我想指出,我不确定您是否想使用
排序uniq
方式。您仍然在存储您并不真正想要存储的元素。在这种情况下,最好使用散列

另外,请。
find\u perms(0345)
的输出可能会让您感到惊讶,但是
find\u perms('0345')
不会

最后,
算法::置换::置换
非常快

考虑到这些因素,我将把您的代码重新编写为:

sort(uniq(@others))
sort(uniq @others)
's
NextPermute
不会产生重复项,因此无需花费内存和CPU来清除它们

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


如果我能接受两个答案,我会的!!我可能会走这条路。谢谢
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