aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2024-08-25 01:18:46 -0400
committerMatthew Neleigh <matthew.neleigh@gmail.com>2024-08-25 01:18:46 -0400
commita4abd3d19e8c3aab875be13b4a907c660f797944 (patch)
tree50d3c5688cebc29a8cc16779fe61164ce98d85a5
parent65ac7b5eef1707ff780c981a1af53c1303b68db2 (diff)
downloadperlweeklychallenge-club-a4abd3d19e8c3aab875be13b4a907c660f797944.tar.gz
perlweeklychallenge-club-a4abd3d19e8c3aab875be13b4a907c660f797944.tar.bz2
perlweeklychallenge-club-a4abd3d19e8c3aab875be13b4a907c660f797944.zip
new file: challenge-283/mattneleigh/perl/ch-1.pl
new file: challenge-283/mattneleigh/perl/ch-2.pl
-rwxr-xr-xchallenge-283/mattneleigh/perl/ch-1.pl75
-rwxr-xr-xchallenge-283/mattneleigh/perl/ch-2.pl82
2 files changed, 157 insertions, 0 deletions
diff --git a/challenge-283/mattneleigh/perl/ch-1.pl b/challenge-283/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..4ee777fc16
--- /dev/null
+++ b/challenge-283/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 3, 3, 1 ],
+ [ 3, 2, 4, 2, 4 ],
+ [ 1 ],
+ [ 4, 3, 1, 1, 1, 4 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ my $rval = find_solitary_element(@{$integer_list});
+
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ defined($rval) ?
+ $rval
+ :
+ 0
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of integers, find one that appears only once
+# Takes one argument:
+# * An array of integers to examine (e.g. ( 3, 3, 1 ) )
+# Returns on success:
+# * An element that appears only once; if more than one element qualifies, the
+# first is returned (e.g. 1 )
+# Returns on error:
+# * undef if no element was found only once
+################################################################################
+sub find_solitary_element{
+
+ my %frequency_table;
+ my $int;
+
+ # Build a frequency table, counting instances
+ # of each element
+ foreach $int (@ARG){
+ $frequency_table{$int}++;
+ }
+
+ # Examine the frequency of each element
+ foreach $int (@ARG){
+ # Found something that appeared once- return
+ # it
+ return($int)
+ if($frequency_table{$int} == 1);
+ }
+
+ # Got here? Must not have found anything that
+ # appeared only once
+ return(undef);
+
+}
+
+
+
diff --git a/challenge-283/mattneleigh/perl/ch-2.pl b/challenge-283/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..66e2e9018a
--- /dev/null
+++ b/challenge-283/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 1, 2, 1, 0 ],
+ [ 0, 3, 0 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOuput: %s\n\n",
+ join(", ", @{$integer_list}),
+ verify_index_quantity_correlation(@{$integer_list}) ?
+ "true"
+ :
+ "false"
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given an array of positive integers, determine wherther the value of each
+# index $i appears within the array a number of times equal to the value stored
+# in the array at that index
+# Takes one argument:
+# * The list of digits eo examine (e.g. (1, 2, 1, 0) )
+# Returns:
+# * 0 if any digit did not appear in the expected quantity
+# * 1 if all digits appeared in the expected quantity, as would be the case for
+# the example above, for 0 appears 1 time, 1 appears 2 times, 2 appears 1
+# time, and 3 appears 0 times.
+################################################################################
+sub verify_index_quantity_correlation{
+
+ my %frequency_table;
+
+ # Build a frequency table, counting instances
+ # of each digit
+ foreach my $digit (@ARG){
+ $frequency_table{$digit}++
+ }
+
+ # Loop over each index in the given array
+ foreach my $i (0 .. $#ARG){
+ # Return zero if the frequency count for this
+ # index does not match the array value at
+ # this index
+ return(0)
+ unless(
+ # Digits we never saw get an effective count
+ # of 0
+ defined($frequency_table{$i}) ?
+ $frequency_table{$i}
+ :
+ 0
+ ==
+ $ARG[$i]
+ );
+ }
+
+ # Got here- every digit occurred in the
+ # correct quantity
+ return(1);
+
+}
+
+
+