aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-15 21:02:54 +0000
committerGitHub <noreply@github.com>2023-11-15 21:02:54 +0000
commit2293c1e40b1ca2a95488a9065469fcb2e02eefd4 (patch)
treef501fc20a810224a5edcd4099986a3279f574613
parent2bf36663688c70da5d1a88a526564650cbac6969 (diff)
parent4dc310b4abca5a770a8e9d7bbe9ba300a8784242 (diff)
downloadperlweeklychallenge-club-2293c1e40b1ca2a95488a9065469fcb2e02eefd4.tar.gz
perlweeklychallenge-club-2293c1e40b1ca2a95488a9065469fcb2e02eefd4.tar.bz2
perlweeklychallenge-club-2293c1e40b1ca2a95488a9065469fcb2e02eefd4.zip
Merge pull request #9065 from mattneleigh/pwc243
new file: challenge-243/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-243/mattneleigh/perl/ch-1.pl64
-rwxr-xr-xchallenge-243/mattneleigh/perl/ch-2.pl64
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-243/mattneleigh/perl/ch-1.pl b/challenge-243/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..c7680880ba
--- /dev/null
+++ b/challenge-243/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 1, 3, 2, 3, 1 ],
+ [ 2, 4, 3, 5, 1 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@nums = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ count_reverse_pairs(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Determine how many reverse pairs are in a list of integers, where a reverse
+# pair is defined as any ($i, $j) within the list where:
+#
+# 1) 0 <= $i < $j <= $#ints
+# 2) $ints[$i] > 2 * $ints[$j].
+#
+# Takes one argument:
+# * A list of integers to examine
+# Returns:
+# * The count of reverse pairs found within the list, where reverse pairs are
+# defined above
+################################################################################
+sub count_reverse_pairs{
+
+ my $count = 0;
+
+ # Loop over every pair within the list
+ for my $i (0 .. $#ARG - 1){
+ for my $j ($i .. $#ARG){
+ # Increment the count if this pair meets
+ # the specified criteria
+ $count++
+ if($ARG[$i] > 2 * $ARG[$j]);
+ }
+ }
+
+ return($count);
+
+}
+
+
+
diff --git a/challenge-243/mattneleigh/perl/ch-2.pl b/challenge-243/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..4c3c69eab4
--- /dev/null
+++ b/challenge-243/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 2, 5, 9 ],
+ [ 7, 7, 7, 7, 7, 7, 7 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@nums = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ calculate_quotient_floor_sum(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the sum of the floor (integer value, rounded down) of the quotients
+# of every possible pair of numbers described by two sets of indices within a
+# list- including those where the indices are the same
+# Takes one argument:
+# * A list of integers to examine
+# Returns:
+# * The sum of the floor (integer value, rounded down) of the quotients of
+# every possible pair of numbers described by two sets of indices within a
+# list- including those where the indices are the same
+################################################################################
+sub calculate_quotient_floor_sum{
+ use POSIX;
+
+ my $sum = 0;
+
+ # Loop such that $i and $j take on every
+ # possible index- even cases where they're
+ # both the same
+ for my $i (0 .. $#ARG){
+ for my $j (0 .. $#ARG){
+ # Add the floor of the quotient of this
+ # pair's members to the sum
+ $sum += floor($ARG[$i] / $ARG[$j]);
+ }
+ }
+
+ return($sum);
+
+}
+
+
+