Peter’s blog ✴ Week 338 ✴ 8 September 2025

THE WEEKLY CHALLENGE
High and far

The Perl Camel

Task 1

Highest row

You are given an $m x $n matrix. Write a script to find the highest row sum in the given matrix.

Examples


Example 1
Input: @matrix = ([4,  4, 4, 4],
                  [10, 0, 0, 0],
                  [2,  2, 2, 9])
Output: 16
Row 1: 4  + 4 + 4 + 4 => 16
Row 2: 10 + 0 + 0 + 0 => 10
Row 3: 2  + 2 + 2 + 9 => 15

Example 2
Input: @matrix = ([1, 5],
                  [7, 3],
                  [3, 5])
Output: 10

Example 3
Input: @matrix = ([1, 2, 3],
                  [3, 2, 1])
Output: 6

Example 4
Input: @matrix = ([2, 8, 7],
                  [7, 1, 3],
                  [1, 9, 5])
Output: 17

Example 5
Input: @matrix = ([10, 20,  30],
                  [5,  5,   5],
                  [0,  100, 0],
                  [25, 25,  25])
Output: 100

Analysis

This is a challenge where there will be a one-liner which is not straightforward to decipher, and as usual I have gone for a more verbose, but more transparent solution.

Perl Weekly’s review

from Perl Weekly issue 738

Peter's solutions are exemplary models of clarity, robustness and efficiency. He deliberately chooses transparent, verbose code over clever one-liners, making his solutions easy to understand and maintain. Furthermore, he demonstrates deep insight by identifying and implementing highly efficient algorithms, especially for the second task.

Try it 

Try running the script with any input:



example: [1, 7, 11], [4, 8, 6], [9, 2, 9]

Script


#!/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);
    }
}

18 lines of code

Output from script


Input:  [4, 4, 4, 4], 
        [10, 0, 0, 0], 
        [2, 2, 2, 9]
Output: 16

Input:  [1, 5], 
        [7, 3], 
        [3, 5]
Output: 10

Input:  [1, 2, 3], 
        [3, 2, 1]
Output: 6

Input:  [10, 20, 30], 
        [5, 5, 5], 
        [0, 100, 0], 
        [25, 25, 25]
Output: 100

Input:  [-1, -2, -3], 
        [-4, -5, -6], 
        [-7, -8, -9]
Output: -6

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain