diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-25 10:14:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-25 10:14:49 +0100 |
| commit | ecaec8aa7a9199f3e093b7c4508dd02266b85c83 (patch) | |
| tree | 811db4c5296801bdfd62a168662c635327494abf | |
| parent | 4a231f1885c6aff7af7e92cdc1c6363b8ced4db6 (diff) | |
| parent | a4abd3d19e8c3aab875be13b4a907c660f797944 (diff) | |
| download | perlweeklychallenge-club-ecaec8aa7a9199f3e093b7c4508dd02266b85c83.tar.gz perlweeklychallenge-club-ecaec8aa7a9199f3e093b7c4508dd02266b85c83.tar.bz2 perlweeklychallenge-club-ecaec8aa7a9199f3e093b7c4508dd02266b85c83.zip | |
Merge pull request #10690 from mattneleigh/pwc283
new file: challenge-283/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-283/mattneleigh/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-283/mattneleigh/perl/ch-2.pl | 82 |
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); + +} + + + |
