aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2024-01-16 14:38:54 -0500
committerMatthew Neleigh <matthew.neleigh@gmail.com>2024-01-16 14:38:54 -0500
commitb1e13994b93e4d4056bfd3fe806b01ec0107558a (patch)
treee911d7beb8ea2a813802cc59e899770177c31ad6
parenta0df2b64bcda24f6ea2b4cd00b9cefe82bcae8ed (diff)
downloadperlweeklychallenge-club-b1e13994b93e4d4056bfd3fe806b01ec0107558a.tar.gz
perlweeklychallenge-club-b1e13994b93e4d4056bfd3fe806b01ec0107558a.tar.bz2
perlweeklychallenge-club-b1e13994b93e4d4056bfd3fe806b01ec0107558a.zip
new file: challenge-252/mattneleigh/perl/ch-1.pl
new file: challenge-252/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-252/mattneleigh/perl/ch-1.pl59
-rwxr-xr-xchallenge-252/mattneleigh/perl/ch-2.pl73
2 files changed, 132 insertions, 0 deletions
diff --git a/challenge-252/mattneleigh/perl/ch-1.pl b/challenge-252/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..71e1afcac9
--- /dev/null
+++ b/challenge-252/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 1, 2, 3, 4 ],
+ [ 2, 7, 1, 19, 18, 3 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ sum_squares_special_elements(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a list of integers, calculate the sum of the squares of all Special
+# Elements within the array, where Special Elements are those at positions in
+# which their 1-indexed, er, index evenly divides into N where N is the number
+# of elements in the list (i.e. I => (1 .. N) and N % I == 0 )
+# Takes one argument:
+# * The list of integers to examine (e.g. ( 2, 7, 1, 19, 18, 3 ) )
+# Returns:
+# * The sum of the suqares of the special elements, as defined above (e.g. 63 )
+################################################################################
+sub sum_squares_special_elements{
+
+ my $sum = 0;
+
+ # Loop from 1 to N + 1
+ for my $i (1 .. $#ARG + 1){
+ # Add the sum of the square of this value if
+ # its adjusted index is Special
+ $sum += $ARG[$i - 1] ** 2
+ unless(scalar(@ARG) % $i);
+ }
+
+ return($sum);
+
+}
+
+
+
diff --git a/challenge-252/mattneleigh/perl/ch-2.pl b/challenge-252/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..62efd1b91a
--- /dev/null
+++ b/challenge-252/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integers = (
+ # Given cases
+ 5, 3, 1,
+
+ # Additional test cases
+ 4, 0
+);
+
+print("\n");
+foreach my $integer (@integers){
+ printf(
+ "Input: \$n = %d\nOutput: (%s)\n\n",
+ $integer,
+ join(", ", create_zero_sum_array($integer))
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a positive integer $n, return an array of $n unique integers that all
+# add up to zero
+# Takes one argument:
+# * A positive integer $n (e.g. 4)
+# Returns:
+# * A list of $n integers that all add up to zero (e.g. ( -1, -2, 2, 1 ) )
+################################################################################
+sub create_zero_sum_array{
+ my $count = shift();
+
+ my @ints = ();
+
+ # See if we have an odd number to deal with, and
+ # if so, add a zero to the list, then decrement
+ # the count
+ if($count % 2){
+ push(@ints, 0);
+ $count--;
+ }
+
+ # Divide the count by two (it will be even now)
+ # then loop until it's decremented to zero
+ $count /= 2;
+ while($count){
+ # Add the current count value, and its negative,
+ # to opposite ends of the array, and decrement
+ # it
+ push(@ints, $count);
+ unshift(@ints, -$count);
+ $count--;
+ }
+
+ return(@ints);
+
+}
+
+
+