aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-21 10:46:27 +0000
committerGitHub <noreply@github.com>2023-11-21 10:46:27 +0000
commitf274932c2e3b1f3fdc50013fa194a9ef2e2bf607 (patch)
tree02f448217eb0c2bffbfe663de2245116307fc154
parent9fb936ed174ebbbd483eef241b30d3de904995c7 (diff)
parent246ff8ec01bb882ed9f538a4b41c0ee3f6702307 (diff)
downloadperlweeklychallenge-club-f274932c2e3b1f3fdc50013fa194a9ef2e2bf607.tar.gz
perlweeklychallenge-club-f274932c2e3b1f3fdc50013fa194a9ef2e2bf607.tar.bz2
perlweeklychallenge-club-f274932c2e3b1f3fdc50013fa194a9ef2e2bf607.zip
Merge pull request #9113 from mattneleigh/pwc244
new file: challenge-244/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-244/mattneleigh/perl/ch-1.pl76
-rwxr-xr-xchallenge-244/mattneleigh/perl/ch-2.pl104
2 files changed, 180 insertions, 0 deletions
diff --git a/challenge-244/mattneleigh/perl/ch-1.pl b/challenge-244/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..cfd23f1cb5
--- /dev/null
+++ b/challenge-244/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 8, 1, 2, 2, 3 ],
+ [ 6, 5, 4, 8 ],
+ [ 2, 2, 2 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@int = (%s)\nOutput: (%s)\n\n",
+ join(", ", @{$integer_list}),
+ join(", ", count_smaller_members(@{$integer_list}))
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Count the number of elements in a list of integers that are smaller than each
+# individual member in the list
+# Takes one argument:
+# * A list of integers (e.g. ( 8, 1, 2, 2, 3 ) )
+# Returns:
+# * A list of counts of members in the supplied list that are smaller than the
+# corresponding member of the original list (e.g. ( 4, 0, 1, 1, 3 ) )
+################################################################################
+sub count_smaller_members{
+
+ # Make a copy of the arguments, sorted in
+ # ascending order
+ my @sorted = sort({ $a <=> $b } @ARG);
+
+ return(
+ map(
+ # Run on each member of the original
+ # list; returns each member's count of
+ # numbers smaller than itself
+ {
+ my $count = 0;
+
+ # Loop over the sorted numbers
+ for my $num (@sorted){
+ # Break out of the loop unless this
+ # sorted member is smaller than the one
+ # being examined; otherwise increment
+ # the count
+ last
+ unless($num < $_);
+ $count++;
+ }
+
+ $count;
+ }
+ @ARG
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-244/mattneleigh/perl/ch-2.pl b/challenge-244/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..57704262b5
--- /dev/null
+++ b/challenge-244/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,104 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 2, 1, 4 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@nums = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ calculate_combination_power_sum(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the sum of the powers of all possible combinations of a list of
+# integers, with the power being defined as the square of the largest number
+# in a sequence, multiplied by the smallest in the sequence
+# Takes one argument:
+# * A list of integers (e.g. (2, 1, 4) )
+# Returns:
+# * The sum of all powers of all possible combinations (see definition above)
+# within the list (e.g. 141)
+################################################################################
+sub calculate_combination_power_sum{
+ use List::Util qw(min max);
+
+ my $sum = 0;
+
+ # Loop over every possible combination of
+ # integers
+ for my $combination (combinations(@ARG)){
+ # Skip the empty list that combinations() can
+ # return (see its documentation)
+ next
+ unless(scalar(@{$combination}));
+
+ # Add to the sum the square of the max value
+ # in this combination list multiplied by the
+ # minimum value in the list
+ $sum += (max(@{$combination}) ** 2) * min(@{$combination});
+ }
+
+ return($sum);
+
+}
+
+
+################################################################################
+# Given a list of N integers, provides a list of all possible combinations of
+# M integers, for all M where 0 <= M <= N
+# Takes one argument:
+# * A list of integers (e.g. (2, 1, 4) )
+# Returns:
+# * A list of all possible combinations of M integers, including where M equals
+# zero (e.g.
+# (
+# [ ],
+# [ 2 ],
+# [ 1 ],
+# [ 2, 1 ],
+# [ 4 ],
+# [ 2, 4 ],
+# [ 1, 4 ],
+# [ 2, 1, 4 ]
+# )
+# )
+#
+# NOTE: Translated and adapted from a Python solution found in the comments at:
+# https://stackoverflow.com/questions/464864/get-all-possible-2n-combinations-of-a-list-s-elements-of-any-length
+################################################################################
+sub combinations{
+
+ return([])
+ unless(scalar(@ARG));
+
+ my @cs;
+
+ for my $c (combinations(@ARG[1 .. $#ARG])){
+ push(@cs, [ @{$c} ], [ $ARG[0], @{$c} ]);
+ }
+
+ return(@cs);
+
+}
+
+
+