diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2023-12-20 16:02:11 -0500 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2023-12-20 16:02:11 -0500 |
| commit | 5cc256c6ea17bdcd1a3164e68cbe916b20b6cba3 (patch) | |
| tree | 9bbdc55d09a870f0185eca678b29b95ddf84211a /challenge-248 | |
| parent | 2c10285a8dbd72808087f6b070d32a0f6902e970 (diff) | |
| download | perlweeklychallenge-club-5cc256c6ea17bdcd1a3164e68cbe916b20b6cba3.tar.gz perlweeklychallenge-club-5cc256c6ea17bdcd1a3164e68cbe916b20b6cba3.tar.bz2 perlweeklychallenge-club-5cc256c6ea17bdcd1a3164e68cbe916b20b6cba3.zip | |
new file: challenge-248/mattneleigh/perl/ch-1.pl
new file: challenge-248/mattneleigh/perl/ch-2.pl
Diffstat (limited to 'challenge-248')
| -rwxr-xr-x | challenge-248/mattneleigh/perl/ch-1.pl | 95 | ||||
| -rwxr-xr-x | challenge-248/mattneleigh/perl/ch-2.pl | 176 |
2 files changed, 271 insertions, 0 deletions
diff --git a/challenge-248/mattneleigh/perl/ch-1.pl b/challenge-248/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..d685799252 --- /dev/null +++ b/challenge-248/mattneleigh/perl/ch-1.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @strings_and_characters = ( + # Given cases + [ "loveleetcode", "e" ], + [ "aaab", "b" ], + + # Additional test cases + [ "perliscool", "x" ] +); + +print("\n"); +foreach my $string_and_character (@strings_and_characters){ + printf( + "Input: \$str = \"%s\", \$char = \"%s\"\nOutput: (%s)\n\n", + @{$string_and_character}, + join( + ", ", + relative_distances_to_character(@{$string_and_character}) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a string and a character, calculate the relative distance from each +# character within the string to the nearest match of the specified character, +# if found +# Takes two arguments: +# * A string to examine (e.g. "loveleetcode") +# * A character to look for in the string (e.g. 'e') +# Returns on success: +# * A list of relative distances from each position in the string to the +# nearest match to the specified character (e.g. +# (3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0) ) +# Returns on error: +# * The empty list (i.e. () ) if the specified character is not found within +# the string +################################################################################ +sub relative_distances_to_character{ + # Required for @LAST_MATCH_START (@-) + use v5.6; + use List::Util qw(min); + + my @matches; + my $current = 0; + + # Attempt to match the character in the + # string, taking note of the location of + # each occurrence + while($ARG[0] =~ m/$ARG[1]/g){ + push(@matches, $LAST_MATCH_START[0]); + } + + # Return an empty list if the character + # wasn't found + return( () ) + unless(@matches); + + return( + # Make a list of relative distances to + # the nearest character match for each + # position within the string + map( + { + $current++ + if($_ > $matches[$current]); + + min( + abs($_ - $matches[$current - 1]), + abs($_ - $matches[$current]) + ); + } + 0 .. length($ARG[0]) - 1 + ) + ); + +} + + + diff --git a/challenge-248/mattneleigh/perl/ch-2.pl b/challenge-248/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..713b002066 --- /dev/null +++ b/challenge-248/mattneleigh/perl/ch-2.pl @@ -0,0 +1,176 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @matrices = ( + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ], + [ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ] +); + +print("\n"); +foreach my $matrix (@matrices){ + printf( + "Input: \$a = [\n%s\n ]\n" . + "Output: \$b = [\n%s\n ]\n\n", + join( + ",\n", + map( + sprintf(" %s", $_), + matrix_to_strings($matrix) + ) + ), + join( + ",\n", + map( + sprintf(" %s", $_), + matrix_to_strings( + submatrix_sums($matrix) + ) + ) + ), + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a matrix, calculate the sums of the values of each 2x2 sub-matrix +# within, and produce a matrix containing those sums +# Takes one argument: +# * A ref to the matrix (e.g. +# [ +# [ 1, 2, 3, 4 ], +# [ 5, 6, 7, 8 ], +# [ 9, 10, 11, 12 ] +# ] +# ) +# Returns: +# * A ref to a matrix containing the sums of the values in each 2x2 sub-matrix +# within the original matrix (e.g. +# [ +# [ 14, 18, 22 ], +# [ 30, 34, 38 ] +# ] +# ) +################################################################################ +sub submatrix_sums{ + my $A = shift(); + + my @B; + + # Loop over both sets of coordinates in the + # matrix, from zero to their max value minus + # one + for my $k (0 .. $#$A - 1){ + for my $i (0 .. $#{$A->[0]} - 1){ + # Sum the four values in each 2x2 sub-matrix + # that begins at $k, $i ('i' and 'k' being + # reversed from typical mathematical + # convention due to the way nested arrays + # work) + $B[$k][$i] = + $A->[$k][$i] + $A->[$k][$i + 1] + + $A->[$k + 1][$i] + $A->[$k + 1][$i + 1]; + } + } + + return(\@B); + +} + + + +################################################################################ +# Given a matrix, produce a set of strings of uniform length and formatting +# containing the contents of the matrix +# Takes one argument: +# * A ref to the matrix (e.g. +# [ +# [ 4, 2 ], +# [ 1, 12 ], +# ] +# ) +# Returns: +# * A list of strings describing the contents of the matrix with uniform length +# and formatting (e.g. +# ( +# "[ 4, 2 ]", +# "[ 1, 12 ]" +# ) +# ) +# Note that strings returned by this function will have square brackets at each +# end, but will neither have commas nor carriage returns to separate the +# rows in printed output, nor will they contain spaces for indenting; if the +# calling code requires any of these, it must provide them itself. +################################################################################ +sub matrix_to_strings{ + use List::Util qw(max); + + # Make a printf() format that will accommodate + # the longest value, textually speaking, in + # the matrix + my $value_format = + "%" + . + # 3: Get the longest length amongst all the + # rows + max( + map( + # 2: Get the longest length in each row + max( + # 1: Get the textual length for each value + map(length($_), @{$_}) + ), + @{$ARG[0]} + ) + ) + . + "d"; + + return( + # 4: Make a list of lines of text containing + # the contents of all matrix rows + map( + # 3: Put square brackets around each row + sprintf( + "[ %s ]", + # 2: Make a string of uniform length out of + # each matrix row + join( + ", ", + # 1: Make a formatted string of uniform length + # out of each matrix value in the row + map( + sprintf($value_format, $_), + @{$_} + ) + ) + ), + @{$ARG[0]} + ) + ); + +} + + + |
