aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2023-04-05 06:44:14 -0700
committerrobbie-hatley <Robbie.Hatley@gmail.com>2023-04-05 06:44:14 -0700
commitd1a80636474c530b07c08886a8a6b896c9421418 (patch)
tree5a687f4de1e78cf9800dace8ecf3ede22be8fad1
parenta3d0f8555cad823c79011a4a0fecd71c4d17f063 (diff)
downloadperlweeklychallenge-club-d1a80636474c530b07c08886a8a6b896c9421418.tar.gz
perlweeklychallenge-club-d1a80636474c530b07c08886a8a6b896c9421418.tar.bz2
perlweeklychallenge-club-d1a80636474c530b07c08886a8a6b896c9421418.zip
made some improvements
-rwxr-xr-xchallenge-211/robbie-hatley/perl/ch-2.pl14
1 files changed, 8 insertions, 6 deletions
diff --git a/challenge-211/robbie-hatley/perl/ch-2.pl b/challenge-211/robbie-hatley/perl/ch-2.pl
index 13325d3912..6daeb97d97 100755
--- a/challenge-211/robbie-hatley/perl/ch-2.pl
+++ b/challenge-211/robbie-hatley/perl/ch-2.pl
@@ -44,21 +44,24 @@ my $db = 0; # Debug?
# Obtain an array of all partitions of a given set into two non-empty
# parts with the size of the first part not-greater-than the size of the
# second part (to avoid duplicate partitions):
-sub two_non_empty ($aref, $partref){
+sub two_non_empty ($aref){
# How big is the original array?
my $size = scalar(@{$aref});
+ # Make an array to hold partitions:
+ my @partitions;
# No need to allow the first part to be more than half the size
# of the array, else we'd get duplicate partitions:
my $limit = int($size/2);
for ( my $n = 1 ; $n <= $limit ; ++$n ){
- my $parts = Set::Partition->new(
+ my $size_n_partitions = Set::Partition->new(
list => $aref,
partition => [$n, $size - $n],
);
- while (my $part = $parts->next) {
- push @{$partref}, $part;
+ while (my $partition = $size_n_partitions->next) {
+ push @partitions, $partition;
}
}
+ return \@partitions;
}
# What is the average of the real numbers in a referred-to array?
@@ -88,8 +91,7 @@ if (@ARGV) {@arrays = eval($ARGV[0])}
ARRAY: for (@arrays){
say '';
say "array = (@$_)";
- my $partitions = [];
- two_non_empty($_, $partitions);
+ my $partitions = two_non_empty($_);
my $equal_average_flag = 0;
# If debugging, print lots of extra diagnostics:
if ($db) {