diff options
| author | Alexander Pankoff <ccntrq@screenri.de> | 2020-11-01 15:49:15 +0100 |
|---|---|---|
| committer | Alexander Pankoff <ccntrq@screenri.de> | 2020-11-01 19:18:14 +0100 |
| commit | 7e38a468ad7d224e003596ed2e172c218b6008a1 (patch) | |
| tree | 9a788db923c696d581531b8d43d58537c006b034 | |
| parent | caee33c77cf068b2fec5cb4b957f92e5f8f1d57b (diff) | |
| download | perlweeklychallenge-club-7e38a468ad7d224e003596ed2e172c218b6008a1.tar.gz perlweeklychallenge-club-7e38a468ad7d224e003596ed2e172c218b6008a1.tar.bz2 perlweeklychallenge-club-7e38a468ad7d224e003596ed2e172c218b6008a1.zip | |
move `combinations` from ch-083 into a module for reuse
| -rwxr-xr-x | challenge-083/alexander-pankoff/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-083/alexander-pankoff/perl/lib/Combinations.pm | 32 |
2 files changed, 37 insertions, 20 deletions
diff --git a/challenge-083/alexander-pankoff/perl/ch-2.pl b/challenge-083/alexander-pankoff/perl/ch-2.pl index 2e3ca72fca..116f701716 100755 --- a/challenge-083/alexander-pankoff/perl/ch-2.pl +++ b/challenge-083/alexander-pankoff/perl/ch-2.pl @@ -7,12 +7,16 @@ use autodie; use feature qw(say signatures); no warnings 'experimental::signatures'; -use Carp qw(croak); use List::Util qw(any first sum0); use Scalar::Util qw(looks_like_number); use Pod::Usage; +use FindBin; +use lib File::Spec->join( $FindBin::RealBin, 'lib' ); + +use Combinations qw(combinations); + pod2usage( -message => "$0: Expects a list of positive numbers", -exitval => 1, @@ -38,25 +42,6 @@ sub flip_array(@numbers) { return 0; } -# returns possible combinations of $length elements from @pool. -sub combinations ( $count, @pool ) { - croak "cannot build combinations with $count elements from a list of " - . scalar(@pool) - . " elements" - if $count > @pool; - return () if $count == 0; - return map { [$_] } @pool if $count == 1; - - my @combinations; - while ( @pool && $count <= @pool ) { - my $elem = shift @pool; - my @sub_combinations = combinations( $count - 1, @pool ); - push @combinations, map { [ $elem, @$_, ] } @sub_combinations; - } - - return @combinations; -} - =pod =head1 NAME diff --git a/challenge-083/alexander-pankoff/perl/lib/Combinations.pm b/challenge-083/alexander-pankoff/perl/lib/Combinations.pm new file mode 100644 index 0000000000..73b22cfa65 --- /dev/null +++ b/challenge-083/alexander-pankoff/perl/lib/Combinations.pm @@ -0,0 +1,32 @@ +package Combinations; +use strict; +use warnings; +use feature qw(signatures); +no warnings 'experimental::signatures'; + +use Carp qw(croak); + +use Exporter qw(import); + +our @EXPORT_OK = qw(combinations); + +# returns possible combinations of $length elements from @pool. +sub combinations ( $count, @pool ) { + croak "cannot build combinations with $count elements from a list of " + . scalar(@pool) + . " elements" + if $count > @pool; + return () if $count == 0; + return map { [$_] } @pool if $count == 1; + + my @combinations; + while ( @pool && $count <= @pool ) { + my $elem = shift @pool; + my @sub_combinations = combinations( $count - 1, @pool ); + push @combinations, map { [ $elem, @$_, ] } @sub_combinations; + } + + return @combinations; +} + +1; |
