加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl算法:Permute和List :: AllUtils(uniq)

发布时间:2020-12-15 23:27:14 所属栏目:大数据 来源:网络整理
导读: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(
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

嗨,

在发现Algorithm::Permute正在产生重复之后,很可能是由于“1”中“1151”的数量,我决定使用uniq.但是,使用不带括号的sort uniq不会产生预期的结果.但排序(uniq(@x))确实如此.是什么赋予了?

解决方法

perldoc -f sort列出了sort函数的三种语法:

sort SUBNAME LIST
sort BLOCK LIST
sort LIST

sort uniq @others匹配排序的SUBNAME LIST语法.它期望uniq是一个比较全局变量$a和$b的函数,并返回< 0,0或> 0来表示$a和$b的相对排序.

它看起来像你期待并想要排序LIST语法,这是你说的时候得到的

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读