【问题标题】:Create combinations from elements in an array从数组中的元素创建组合
【发布时间】:2017-01-30 15:31:00
【问题描述】:

我有一个如下的数组引用:

my $strings = [qw(a b c d)];

我想形成所有可能的组合并创建一个数组数组:

my $output = [qw(qw([a],[b],[c],[d],[a,b],[a,c],[a,d],[b,c],[b,d],[c,d],   [a,b,c],[a,b,d],[b,c,d],[a,b,c,d]))]

我尝试了什么:

foreach my $n(1..scalar(@array)) {
    my $iter = combinations($strings, $n);
    while (my $c = $iter->next) {
        print "@$c\n";
    }
}

【问题讨论】:

标签: arrays perl perl-module perl-data-structures


【解决方案1】:

如果您手头没有模块,并且不关心外层顺序:

sub fu { 
  my ($base,@rest) = @_;
  my @result = @$base && $base || ();
  push @result, fu( [@$base, shift @rest], @rest) while @rest;
  return @result;
}
my @output = fu([],qw(a b c d));

@output的内容:

[["a"],["a","b"],["a","b","c"],["a","b","c","d"],["a","b","d"],["a","c"],["a","c","d"],["a","d"],["b"],["b","c"],["b","c","d"],["b","d"],["c"],["c","d"],["d"]]

【讨论】:

    【解决方案2】:

    Math::Combinatorics

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Math::Combinatorics qw(combine);
    use Data::Dumper;
    
    my @n = qw(a b c d);
    my @res;
    push @res, combine($_, @n) foreach (0..@n);
    print Dumper(\@res);
    

    输出:

    $VAR1 = [
              [
                'b'
              ],
              [
                'c'
              ],
              [
                'a'
              ],
              [
                'd'
              ],
              [
                'c',
                'a'
              ],
              [
                'c',
                'd'
              ],
              [
                'c',
                'b'
              ],
              [
                'a',
                'd'
              ],
              [
                'a',
                'b'
              ],
              [
                'd',
                'b'
              ],
              [
                'b',
                'a',
                'd'
              ],
              [
                'b',
                'a',
                'c'
              ],
              [
                'b',
                'd',
                'c'
              ],
              [
                'a',
                'd',
                'c'
              ],
              [
                'b',
                'c',
                'd',
                'a'
              ]
            ];
    

    【讨论】:

      【解决方案3】:

      使用Algorithm::Combinatorics 查找所有组合。

      #!/#!/usr/bin/perl
      use strict;
      use warnings;
      use Data::Dumper;
      use Algorithm::Combinatorics qw(combinations);
      my @data = qw(a b c d);
      my $all_combinations;
      foreach (1..4){
          push @$all_combinations, combinations(\@data, $_);
      }
      print Dumper $all_combinations;
      

      输出:

      $VAR1 = [
                [
                  'a'
                ],
                [
                  'b'
                ],
                [
                  'c'
                ],
                [
                  'd'
                ],
                [
                  'a',
                  'b'
                ],
                [
                  'a',
                  'c'
                ],
                [
                  'a',
                  'd'
                ],
                [
                  'b',
                  'c'
                ],
                [
                  'b',
                  'd'
                ],
                [
                  'c',
                  'd'
                ],
                [
                  'a',
                  'b',
                  'c'
                ],
                [
                  'a',
                  'b',
                  'd'
                ],
                [
                  'a',
                  'c',
                  'd'
                ],
                [
                  'b',
                  'c',
                  'd'
                ],
                [
                  'a',
                  'b',
                  'c',
                  'd'
                ]
              ];
      

      【讨论】:

        【解决方案4】:

        您可以使用模块“Algorithm::Combinatorics”

        use Algorithm::Combinatorics "variations_with_repetition";
        my @Variations = variations_with_repetition([qw(a b c d)], 4);
        print "@$_\n", for @Variations;
        

        【讨论】:

        • 您的代码输出与 OP 要求的不同。
        猜你喜欢
        • 1970-01-01
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2022-12-06
        • 2022-01-01
        相关资源
        最近更新 更多