aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2023-08-01 19:33:26 -0400
committerMatthew Neleigh <matthew.neleigh@gmail.com>2023-08-01 19:33:26 -0400
commitc61e8fcd3ed756407f19452a666d4832ddf8ba1c (patch)
tree148eb7061c822c9a4716138dfa2e3f26266234de
parent107a44513e966e7cd37501766c573e3c97688680 (diff)
downloadperlweeklychallenge-club-c61e8fcd3ed756407f19452a666d4832ddf8ba1c.tar.gz
perlweeklychallenge-club-c61e8fcd3ed756407f19452a666d4832ddf8ba1c.tar.bz2
perlweeklychallenge-club-c61e8fcd3ed756407f19452a666d4832ddf8ba1c.zip
new file: challenge-228/mattneleigh/perl/ch-1.pl
new file: challenge-228/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-228/mattneleigh/perl/ch-1.pl73
-rwxr-xr-xchallenge-228/mattneleigh/perl/ch-2.pl73
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-228/mattneleigh/perl/ch-1.pl b/challenge-228/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..5f80039684
--- /dev/null
+++ b/challenge-228/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 2, 1, 3, 2],
+ [ 1, 1, 1, 1],
+ [ 2, 1, 3, 4]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@int = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ sum_unique_integers(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the sum of the unique elements in an array of integers
+# Takes one argument:
+# * An array of integers (e.g. ( 2, 1, 3, 2 ) )
+# Returns:
+# * The sum of all numbers that appear only once in the array (e.g. 4 )
+################################################################################
+sub sum_unique_integers{
+
+ my %counts;
+ my $total = 0;
+
+ # Loop over each number in the list
+ foreach my $num (@ARG){
+ # Skip numbers that have been seen twice
+ # (unseen numbers are undef; numbers seen
+ # once are zero; numbers seen twice are
+ # one)
+ next
+ if($counts{$num});
+
+ if(defined($counts{$num})){
+ # Second time seeing this number-
+ # subtract it from the total and mark
+ # it to be skipped
+ $total -= $num;
+ $counts{$num} = 1;
+ } else{
+ # First time seeing this number- add it
+ # to the total and mark it as seen once
+ $total += $num;
+ $counts{$num} = 0;
+ }
+ }
+
+ return($total);
+
+}
+
+
+
diff --git a/challenge-228/mattneleigh/perl/ch-2.pl b/challenge-228/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..2b2abae864
--- /dev/null
+++ b/challenge-228/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 3, 4, 2 ],
+ [ 1, 2, 3 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@int = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ count_steps_required_for_array_reduction(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Count the steps required to reduce an array of unique integers to an empty
+# list by applying the following rules:
+# 1) If the first element is the smallest in the array, remove it from the
+# array
+# 2) If the first element is NOT the smallest, move it to the end of the array
+# Takes one argument:
+# * The array to examine (e.g. (3, 4, 2) )
+# Returns:
+# * The number of steps required to process the array as described above (e.g.
+# 5 )
+################################################################################
+sub count_steps_required_for_array_reduction{
+
+ # Get a copy of @ARG, sorted in ascending
+ # order
+ my @mins = sort(@ARG);
+ my $ops = 0;
+
+ # Loop while there are still numbers
+ # in @ARG
+ while(@ARG){
+ # Examine the first value in @ARG
+ if($ARG[0] == $mins[0]){
+ # A minimum value- discard it, and the
+ # corresponding minimum
+ shift(@ARG);
+ shift(@mins);
+ } else{
+ # Not a minimum value- move it to the
+ # end of @ARG
+ push(@ARG, shift(@ARG));
+ }
+ $ops++;
+ }
+
+ return($ops);
+
+}
+
+
+