diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-20 14:53:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-20 14:53:17 +0100 |
| commit | 1ec2fb58f51b4568417e9dae57fda601d6e76077 (patch) | |
| tree | 7381c0ed76f77a1b9e6549de28e3af58b05dcd73 | |
| parent | da93bacfdc0d8a5eb608637cabfe05eed0d1ff6d (diff) | |
| parent | c1350941bc8ed095da1112e5e8be74bc9a606513 (diff) | |
| download | perlweeklychallenge-club-1ec2fb58f51b4568417e9dae57fda601d6e76077.tar.gz perlweeklychallenge-club-1ec2fb58f51b4568417e9dae57fda601d6e76077.tar.bz2 perlweeklychallenge-club-1ec2fb58f51b4568417e9dae57fda601d6e76077.zip | |
Merge pull request #8739 from mattneleigh/pwc235
new file: challenge-235/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-235/mattneleigh/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-235/mattneleigh/perl/ch-2.pl | 77 |
2 files changed, 156 insertions, 0 deletions
diff --git a/challenge-235/mattneleigh/perl/ch-1.pl b/challenge-235/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..a4975a832c --- /dev/null +++ b/challenge-235/mattneleigh/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + # Given cases + [ 0, 2, 9, 4, 6 ], + [ 5, 1, 3, 2 ], + [ 2, 2, 3 ], + + # Additional test case(s) + [ 1, 2, 3, 4, 5, 6 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: %s\n\n", + join(", ", @{$integer_list}), + array_has_one_non_increasing_element(@{$integer_list}) ? + "true" + : + "false" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether an array of numbers has one (and only one) non-increasing +# element; that is to say, there is one element $i such that +# $array[$i-1] >= $array[$i] +# Takes one argument: +# * A list of numbers to examine +# Returns: +# * 1 if there is one (and only one) non-increasing value in the array +# * 0 if there are zero, two, or more non-increasing values in the array +################################################################################ +sub array_has_one_non_increasing_element{ + + my $non_increasing_seen = 0; + + # Loop over the array from 1 to n-1 + for my $i (1 .. $#ARG){ + unless($ARG[$i - 1] < $ARG[$i]){ + # Previous value was not lower than the + # current value... + + # See if we've already encountered such + # a situation, and return 0 if we have + return(0) + if($non_increasing_seen); + + # Note that weve seen a non-increasing + # value + $non_increasing_seen = 1; + } + } + + # This will be 1 if there was one + # non-increasing pair, or 0 if there were + # none + return($non_increasing_seen); + +} + + + diff --git a/challenge-235/mattneleigh/perl/ch-2.pl b/challenge-235/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..c0507367c9 --- /dev/null +++ b/challenge-235/mattneleigh/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + # Given cases + [ 1, 0, 2, 3, 0, 4, 5, 0 ], + [ 1, 2, 3 ], + [ 0, 3, 0, 4, 5 ], + + # Additional test case(s) + [ 1, 2, 3, 4, 0 ], + [ 1, 0, 3, 0, 5 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOuput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", duplicate_zeros_with_constant_length(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Duplicate zeros within an array of numbers, shifting all subsequent elements +# to the right, while preserving the original length of the array by deleting +# the rightmost element each time +# Takes one argument: +# * An array of numbers to examine (e.g. ( 1, 0, 2, 3, 0, 4, 5, 0 ) ) +# Returns: +# * A copy of the array, modified as described above (e.g. +# ( 1, 0, 0, 2, 3, 0, 0, 4 ) ) +################################################################################ +sub duplicate_zeros_with_constant_length{ + + my $i = 0; + + # Loop over all @ARG except the last element; + # any changes made there should be deleted + # anyway + while($i < $#ARG){ + unless($ARG[$i]){ + # This value is zero... + # Get rid of the last element in the array + pop(@ARG); + + # Replace the zero at $i with two zeros, + # thus returning the array to its original + # size, and increment $i en passant to skip + # over the zero we just added + splice(@ARG, $i++, 1, (0, 0)); + } + + # Move on to the next element + $i++; + } + + return(@ARG); + +} + + + |
