diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-09 12:55:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-09 12:55:15 +0100 |
| commit | f0e48168a72ddf12b6741d5e9e7bd75dff7f8a99 (patch) | |
| tree | 6b6b65f18eecf55b196149aa2d64453f416d65b4 | |
| parent | 6451ece29a5264187fb8246ed798e00beb1e8107 (diff) | |
| parent | 0ec3380569b90b7afac7890eb88052bde9951e1c (diff) | |
| download | perlweeklychallenge-club-f0e48168a72ddf12b6741d5e9e7bd75dff7f8a99.tar.gz perlweeklychallenge-club-f0e48168a72ddf12b6741d5e9e7bd75dff7f8a99.tar.bz2 perlweeklychallenge-club-f0e48168a72ddf12b6741d5e9e7bd75dff7f8a99.zip | |
Merge pull request #12655 from mattneleigh/pwc338
new file: challenge-338/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-338/mattneleigh/perl/ch-1.pl | 166 | ||||
| -rwxr-xr-x | challenge-338/mattneleigh/perl/ch-2.pl | 94 |
2 files changed, 260 insertions, 0 deletions
diff --git a/challenge-338/mattneleigh/perl/ch-1.pl b/challenge-338/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..3f2c9ef5ea --- /dev/null +++ b/challenge-338/mattneleigh/perl/ch-1.pl @@ -0,0 +1,166 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ +my @matrices = ( + [ + [4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9] + ], + [ + [1, 5], + [7, 3], + [3, 5] + ], + [ + [1, 2, 3], + [3, 2, 1] + ], + [ + [2, 8, 7], + [7, 1, 3], + [1, 9, 5] + ], + [ + [10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25] + ] +); + +print("\n"); +foreach my $matrix (@matrices){ + printf( + "Input: \@matrix = (\n%s\n )\nOutput: %d\n\n", + join( + "\n", + map( + " " . $_, + matrix_to_strings($matrix) + ) + ), + max_row_sum($matrix) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a matrix of numerical data, find the maximum among the sums of all the +# values in each row +# Takes one argument: +# * A ref to a 2D array representing a matrix (e.g. +# [ +# [10, 20, 30], +# [5, 5, 5], +# [0, 100, 0], +# [25, 25, 25] +# ] +# ) +# Returns +# * The maximum among the sums of all the values in each row (e.g. 100) +################################################################################ +sub max_row_sum{ + use List::Util qw(sum max); + + return( + # 2) Find the maximum among the row-sums + max( + map( + # 1) Sum all the members of each row + sum(@{$_}), + @{shift()} + ) + ) + ); + +} + + + +################################################################################ +# 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]} + ) + ) + . + "s"; + + 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]} + ) + ); + +} + + + diff --git a/challenge-338/mattneleigh/perl/ch-2.pl b/challenge-338/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..6604753ba6 --- /dev/null +++ b/challenge-338/mattneleigh/perl/ch-2.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @array_pairs = ( + [ + [ 4, 5, 7 ], + [ 9, 1, 3, 4 ] + ], + [ + [ 2, 3, 5, 4 ], + [ 3, 2, 5, 5, 8, 7 ] + ], + [ + [ 2, 1, 11, 3 ], + [ 2, 5, 10, 2 ] + ], + [ + [ 1, 2, 3 ], + [ 3, 2, 1 ] + ], + [ + [ 1, 0, 2, 3 ], + [ 5, 0 ] + ] +); + +print("\n"); +foreach my $array_pair (@array_pairs){ + printf( + "Input: \@arr1 = (%s)\n \@arr2 = (%s)\nOutput: %d\n\n", + join(", ", @{$array_pair->[0]}), + join(", ", @{$array_pair->[1]}), + max_array_difference($array_pair) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two arrays, find the maximum difference among all pairs of values, such +# that one is taken from each array +# Takes one argument: +# * A ref to a 2D array that contains the two arrays to examine (e.g. +# [ +# [ 2, 3, 5, 4 ], +# [ 3, 2, 5, 5, 8, 7 ] +# ] +# ) +# Returns: +# * The maximum difference among all pair of values, such that one is taken +# from each array (e.g. 6) +################################################################################ +sub max_array_difference{ + use List::Util qw(max); + + my $n; + + return( + # 2) Find the maximum of the differences + max( + # 1) Find the absolute values of the + # differences between each element of + # the first array and each elemwnt of + # the second + map( + { + $n = $_; + + map( + abs($n - $_), + @{$ARG[0][1]} + ); + } + @{$ARG[0][0]} + ) + ) + ); + +} + + + |
