aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-262/mattneleigh/perl/ch-1.pl70
-rwxr-xr-xchallenge-262/mattneleigh/perl/ch-2.pl91
2 files changed, 161 insertions, 0 deletions
diff --git a/challenge-262/mattneleigh/perl/ch-1.pl b/challenge-262/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..490746baca
--- /dev/null
+++ b/challenge-262/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ -3, 1, 2, -1, 3, -2, 4 ],
+ [ -1, -2, -3, 1 ],
+ [ 1,2 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ maximum_instances_by_sign(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Determine the larger of the count of positive values or negative values
+# within a list of integers
+# Takes one argument:
+# * A list of integers to examine (e.g. ( -3, 1, 2, -1, 3, -2, 4 ) )
+# Returns:
+# * The larger of the count of positive values or negative values (e.g. 4, as
+# there are four positive numbers, vs. three negative numbers)
+# NOTE: As zero is neither positive nor negative, it is always ignored for
+# counting purposes
+################################################################################
+sub maximum_instances_by_sign{
+
+ my $pos = 0;
+ my $neg = 0;
+
+ foreach my $num (@ARG){
+ # Zero is neither positive nor negative
+ # so it would not be counted here;
+ # otherwise update pos and neg totals
+ $pos++
+ if($num > 0);
+ $neg++
+ if($num < 0);
+ }
+
+ # Return the larger of the two totals
+ return(
+ $pos > $neg ?
+ $pos
+ :
+ $neg
+ );
+
+}
+
+
+
diff --git a/challenge-262/mattneleigh/perl/ch-2.pl b/challenge-262/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..abcee996a9
--- /dev/null
+++ b/challenge-262/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,91 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @data_sets = (
+ [
+ [ 3, 1, 2, 2, 2, 1, 3 ],
+ 2
+ ],
+ [
+ [ 1, 2, 3 ],
+ 1
+ ]
+);
+
+print("\n");
+foreach my $data_set (@data_sets){
+ printf(
+ "Input: \@ints = (%s) and \$k = %d\nOutput: %d\n\n",
+ join(", ", @{$data_set->[0]}),
+ $data_set->[1],
+ count_equal_pairs_with_divisible_index_products($data_set)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of integers @ints and an integer divisor $k, count the pairs
+# $i and $j within the array that satisfy the following conditions:
+#
+# * 0 <= $i < $j <= $#ints
+# * $ints[$i] == $ints[$j]
+# * ($i * $j) % $k == 0 (product of $i and $j is divisible by $k)
+#
+# Takes one argument:
+# * A ref to a data structure that contains a list of integers, and an integer
+# divisor (e.g.
+# [
+# # The array of ints
+# [ 3, 1, 2, 2, 2, 1, 3 ],
+# # The divisor
+# 2
+# ]
+# )
+# Returns:
+# * The count of pairs within the integer list that are equal and whose indices
+# have a product that is evenly divisible by the divisor
+################################################################################
+sub count_equal_pairs_with_divisible_index_products{
+ use integer;
+
+ my $ints = $ARG[0][0];
+ my $k = $ARG[0][1];
+ my $i;
+ my $j;
+ my $pairs = 0;
+
+ # Loop over all pairs within the
+ # interger list
+ for $i (0 .. $#$ints - 1){
+ for $j ($i + 1 .. $#$ints){
+ # Increment the count if the values at
+ # $i and $j are qual and the product of
+ # $i and $j can be divided evenly by $k
+ $pairs++
+ if(
+ ($ints->[$i] == $ints->[$j])
+ &&
+ !(($i * $j) % $k)
+ );
+ }
+ }
+
+ return($pairs);
+
+}
+
+
+