diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-09-08 12:56:46 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-09-08 12:56:46 +0100 |
| commit | e37c5cfc2ecea6c92ad7a561e90944e6059551c0 (patch) | |
| tree | 7e3dec68787bb7738511cc063dc5533f9f625567 | |
| parent | cb150b4089bfaa27339dfd6bb4e51b580b12c32b (diff) | |
| download | perlweeklychallenge-club-e37c5cfc2ecea6c92ad7a561e90944e6059551c0.tar.gz perlweeklychallenge-club-e37c5cfc2ecea6c92ad7a561e90944e6059551c0.tar.bz2 perlweeklychallenge-club-e37c5cfc2ecea6c92ad7a561e90944e6059551c0.zip | |
Week 338 - High and far
| -rw-r--r-- | challenge-338/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-338/peter-campbell-smith/perl/ch-1.pl | 49 | ||||
| -rwxr-xr-x | challenge-338/peter-campbell-smith/perl/ch-2.pl | 29 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-338/peter-campbell-smith/blog.txt b/challenge-338/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4a858479da --- /dev/null +++ b/challenge-338/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/338 diff --git a/challenge-338/peter-campbell-smith/perl/ch-1.pl b/challenge-338/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..a16aca9ef6 --- /dev/null +++ b/challenge-338/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-08 +use utf8; # Week 338 - task 1 - Highest row +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +highest_row(([4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9])); +highest_row(([1, 5], [7, 3], [3, 5])); +highest_row(([1, 2, 3], [3, 2, 1])); +highest_row(([10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25])); +highest_row(([-1, -2, -3], [-4, -5, -6], [-7, -8, -9])); + +sub highest_row { + + my (@matrix, $highest, $row_sum, $row); + + # initialise + @matrix = @_; + $highest = -Inf; + + # find row sums + for $row (0 .. scalar @matrix - 1) { + $row_sum = 0; + $row_sum += $_ for @{$matrix[$row]}; + $highest = $row_sum if $row_sum > $highest; + } + + # report + say ''; + print_matrix(qq[Input: ], \@matrix); + say qq[Output: $highest]; +} + +sub print_matrix { + + my ($legend, $matrix, $j); + + # format matrix + ($legend, $matrix) = @_; + for $j (0 .. @$matrix - 1) { + print qq{$legend [} . join(', ', @{$matrix->[$j]}) . qq(]); + say $j == @$matrix - 1 ? '' : ', '; + $legend = ' ' x length($legend); + } +}
\ No newline at end of file diff --git a/challenge-338/peter-campbell-smith/perl/ch-2.pl b/challenge-338/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..7256854161 --- /dev/null +++ b/challenge-338/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-08 +use utf8; # Week 338 - task 2 - Max distance +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +max_distance([4, 5, 7], [9, 1, 3, 4]); +max_distance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]); +max_distance([2, 1, 11, 3], [2, 5, 10, 2]); +max_distance([1, 2, 3], [3, 2, 1]); +max_distance([1, 0, 2, 3], [5, 0]); + +sub max_distance { + + my (@one, @two, $diff1, $diff2); + + # initialise + @one = sort {$a <=> $b} @{$_[0]}; + @two = sort {$a <=> $b} @{$_[1]}; + $diff1 = abs($one[0] - $two[-1]); + $diff2 = abs($one[-1] - $two[0]); + + say qq{\nInput: [} . join(', ', @{$_[0]}) . '], [' . join(', ', @{$_[1]}) . ']'; + say qq[Output: ] . ($diff1 > $diff2 ? $diff1 : $diff2); +} |
