diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-01 14:16:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-01 14:16:20 +0000 |
| commit | 7daf3346d20ec5267dab9975bc9bf99e8a739674 (patch) | |
| tree | ef88f308858838126792bcb71ee17912c5d98726 | |
| parent | 504cd39f25daaf12df4133ad416b0077a5e8e4bd (diff) | |
| parent | e56c8aaa77f757cdb6bb24cbeca1b17b174820b6 (diff) | |
| download | perlweeklychallenge-club-7daf3346d20ec5267dab9975bc9bf99e8a739674.tar.gz perlweeklychallenge-club-7daf3346d20ec5267dab9975bc9bf99e8a739674.tar.bz2 perlweeklychallenge-club-7daf3346d20ec5267dab9975bc9bf99e8a739674.zip | |
Merge pull request #12953 from mattneleigh/pwc345
new file: challenge-345/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-345/mattneleigh/perl/ch-1.pl | 81 | ||||
| -rwxr-xr-x | challenge-345/mattneleigh/perl/ch-2.pl | 89 |
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-345/mattneleigh/perl/ch-1.pl b/challenge-345/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..0f206fa4f8 --- /dev/null +++ b/challenge-345/mattneleigh/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 1, 3, 2 ], + [ 2, 4, 6, 5, 3 ], + [ 1, 2, 3, 2, 4, 1 ], + [ 5, 3, 1 ], + [ 1, 5, 1, 5, 1, 5, 1 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", find_peaks(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of integers, determine which values are peaks- values which are +# greater than the adjacent values on either side, or greater than the value on +# the inward side if the value is at either end of the array +# Takes one argument: +# * A list of integers to examine (e.g. (1, 2, 3, 2, 4, 1) ) +# Returns: +# * A list of indices of peaks found within the supplied list (e.g. (2, 4) ) +################################################################################ +sub find_peaks{ + + my $i = 1; + my @peaks; + + # See if the first value is a peak + if($ARG[0] > $ARG[1]){ + # Store this index and skip over the next one + push(@peaks, 0); + $i++; + } + + # Loop while the index is within the array + # but NOT at either end + while($i < $#ARG){ + if(($ARG[$i - 1] < $ARG[$i]) && ($ARG[$i] > $ARG[$i + 1])){ + # This mid-array value is a peak... + # Store this index and skip over the next one, + # which we know can't be a peak + push(@peaks, $i); + $i++; + } + + # Advance within the list + $i++; + } + + # If we didn't skip over the end of the array and + # the last element is a peak, store its index + push(@peaks, $i) + if(($i == $#ARG) && ($ARG[-1] > $ARG[-2])); + + return(@peaks); + +} + + + diff --git a/challenge-345/mattneleigh/perl/ch-2.pl b/challenge-345/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..968312b9d5 --- /dev/null +++ b/challenge-345/mattneleigh/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 5, -1, -1 ], + [ 3, 7, -1, -1, -1 ], + [ 2, -1, 4, -1, -1 ], + [ 10, 20, -1, 30, -1, -1 ], + [ -1, -1, 5, -1 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", process_ints(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given an array of integers, in which every element is either positive or -1, +# step through the array, applying the following rules: +# 1) If the current integer is positive, place it at the front of an array +# @seen +# 2) If the current integer is -1: +# 2a) If the length of @seen is greater than the number of consecutive +# -1s seen before this one ($neg_count), append $seen[$neg_count] to an +# array @ans +# 2b) Or append -1 to @ans +# After all input integers have been processed, the array @ans will be returned +# Takes one argument: +# * The array of integers to process (e.g. (3, 7, -1, -1, -1) ) +# Returns: +# * The array @ans, constructed as described above (e.g. (7, 3, -1) ) +################################################################################ +sub process_ints{ + + my @seen; + my @ans; + my $neg_count = 0; + + # Loop over each int + foreach my $int (@ARG){ + if($int == -1){ + # This is -1: + # Store the appropriate value at the end of + # @seen, as prescribed by the rules stated + # above + push( + @ans, + $neg_count < scalar(@seen) ? + $seen[$neg_count] + : + -1 + ); + + # Increment the negative count + $neg_count++; + } else{ + # This is not -1: + # Store this int at the front of @seen + unshift(@seen, $int); + + # Reset the negative count + $neg_count = 0; + } + } + + return(@ans); + +} + + + |
