**THE WEEKLY CHALLENGE - 248**

https://theweeklychallenge.org/blog/perl-weekly-challenge-248/

## Table of Contents

#### Intrigued by the idea of crafting solutions in different programming languages? Let’s dive into two fascinating coding challenges!

Code: https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-248/lubos-kolouch

# Task 1 - Shortest Distance

#### The Universal Challenge: A Tale of Strings and Distances

In the realm of programming, certain problems present a universal challenge, offering a unique window to explore the strengths of different languages. One such problem is the "Shortest Distance" challenge, which involves calculating the minimum distance of each character in a string from a specified character. This problem isn't just about finding a solution; it's a journey through the landscapes of Python, Perl, and Raku, each offering its distinct tools and idioms.

#### Python: The Clarity Champion

Python, known for its readability and efficiency, tackles this problem with elegance. The solution leverages list comprehensions – a quintessential Python feature that allows for concise and readable iterations. Here, the beauty lies in Python’s ability to express complex operations in a few lines of code, a testament to its design philosophy of simplicity and readability.

The algorithm in Python takes two steps:

**Identify Positions:**First, we find the indices of the target character using a list comprehension.**Calculate Distances:**Then, for each index in the string, we compute the minimum distance to any of the identified positions, again using a list comprehension.

This approach is not just efficient; it’s Pythonic – aligning with the language’s emphasis on readability and straightforwardness.

#### Perl: The Text Processing Powerhouse

Perl shines when it comes to text manipulation, and this challenge is no exception. The Perl solution exploits regular expressions and map functions, showcasing Perl’s prowess in handling text-related tasks. The key here is Perl’s ability to handle complex text processing with ease and efficiency.

Similar to the Python solution, the Perl approach involves:

**Finding Target Character Indices:**Utilizing grep and regular expressions, Perl efficiently locates the positions of the target character.**Computing Shortest Distances:**Through a map function combined with a minimum distance calculation, Perl effectively finds the shortest distance for each character in the string.

Perl’s solution underlines the language’s strengths in text processing, offering a robust and efficient approach to the problem.

#### Raku: The Evolutionary Leap

Raku, the successor to Perl 5, brings in more powerful features and a more consistent syntax. In solving this challenge, Raku’s expressiveness and functional programming capabilities come to the fore.

The Raku solution, like its counterparts, involves two steps:

**Locate Character Positions:**Using Raku’s comb method, the solution elegantly finds the positions of the target character.**Determine Minimum Distances:**Employing Raku's map and min methods, the solution adeptly computes the shortest distances.

Raku’s approach highlights the language’s modern features and its ability to handle complex tasks with expressive and concise code.

#### Emphasizing Testing: The Seal of Reliability

A crucial aspect of these solutions is the inclusion of tests – a practice that reinforces reliability and correctness in software development. Python, Perl, and Raku each offer testing frameworks that are used to validate the solutions, ensuring they work as expected. This emphasis on testing resonates with the principles of operational excellence and practical problem-solving.

#### Conclusion: A Symphony of Languages

This exploration into solving the "Shortest Distance" challenge in Python, Perl, and Raku is more than a comparison of programming languages. It’s a testament to the diverse approaches and philosophies inherent in these languages. Python’s readability, Perl’s text processing capabilities, and Raku’s expressive syntax each bring unique strengths to the table.

# Task 2 - Submatrix Sum

Imagine you're given a matrix `A`

of size NxM. Your task is to create a new matrix `B`

of size (N-1)x(M-1), where each element in `B`

is the sum of a 2x2 submatrix of `A`

. It's a practical problem that tests your ability to handle arrays and nested loops efficiently.

## Perl Approach: Embracing Elegance and Simplicity

Perl, known for its text processing prowess, also excels in handling data structures like matrices. Here's how you can tackle this problem in Perl:

```
sub construct_matrix {
my ($a) = @_;
my @b;
for my $i (0 .. @$a - 2) {
for my $k (0 .. @{$a->[$i]} - 2) {
$b[$i][$k] = $a->[$i][$k] + $a->[$i][$k + 1] + $a->[$i + 1][$k] + $a->[$i + 1][$k + 1];
}
}
return \@b;
}
```

The elegance of Perl shines in its straightforward syntax for accessing and iterating over arrays. We simply iterate through each element, summing the relevant 2x2 submatrix, and populate the new matrix `B`

.

## Python Solution: Clarity Meets Efficiency

Python, a language revered for its readability and simplicity, handles this task with equal finesse. Here's how:

```
def construct_matrix(a: List[List[int]]) -> List[List[int]]:
n, m = len(a), len(a[0])
b = [[sum(a[i + di][j + dj] for di in range(2) for dj in range(2))
for j in range(m - 1)] for i in range(n - 1)]
return b
```

Python's list comprehensions and the `sum`

function make this code compact and readable. The solution traverses the matrix `A`

, calculates the sum of each 2x2 block, and constructs matrix `B`

in a declarative style.

## Raku Solution: A Glimpse into the Future of Programming

Raku, formerly known as Perl 6, brings a modern twist to Perl's philosophy. Here's the Raku way:

```
sub construct-matrix(@a) {
my @b;
for 0..@a.end - 1 -> $i {
for 0..@a[$i].end - 1 -> $k {
@b[$i][$k] = @a[$i][$k] + @a[$i][$k + 1] + @a[$i + 1][$k] + @a[$i + 1][$k + 1];
}
}
return @b;
}
```

Raku's syntax is both expressive and concise, offering features like easy array slicing and a more intuitive loop syntax. This makes the code not only efficient but also a pleasure to read and write.

## Testing: Ensuring Reliability

Testing is crucial. Each language version includes tests to verify correctness. Perl uses `Test::More`

, Python employs `unittest`

, and Raku utilizes its built-in testing framework. These tests check if the function correctly computes the sum of submatrices for given inputs.