THE WEEKLY CHALLENGE - 242
https://theweeklychallenge.org/blog/perl-weekly-challenge-242/
Table of Contents
Task 1 - Missing Members
Imagine you've got two lists of numbers, and you need to quickly figure out what's missing from each list compared to the other. Sounds like a puzzle, right? 🧩 Let's explore a fascinating coding challenge that not only tests your logic but also showcases the beauty of programming in Perl, Python, and Raku!
Content: In the realm of data analysis and software development, comparing lists or arrays is a common task. This can range from checking inventory lists to comparing datasets in scientific research. Today, we're tackling a challenge where we compare two arrays and find the unique elements missing in each.
The algorithm we employ is elegant yet straightforward. It's all about utilizing the power of hash tables (or dictionaries, in Python parlance) and set operations. The idea is to count the occurrences of each element in both arrays (using a hash table) or directly find the unique elements (using sets).
Perl Solution: In Perl, we use a hash to keep track of the counts of elements in both arrays. Then, we find elements that are missing in each array by checking the count.
sub find_missing_members {
my ($arr1_ref, $arr2_ref) = @_;
my %count;
$count{$_}++ for @$arr1_ref;
$count{$_}-- for @$arr2_ref;
my @missing_in_arr2 = sort { $a <=> $b } grep { $count{$_} > 0 } keys %count;
my @missing_in_arr1 = sort { $a <=> $b } grep { $count{$_} < 0 } keys %count;
return (\@missing_in_arr2, \@missing_in_arr1);
}
Python Solution: In Python, we employ set operations to find the unique missing members. This approach is concise and leverages the powerful set data structure available in Python.
def find_missing_members(arr1, arr2):
set_arr1, set_arr2 = set(arr1), set(arr2)
missing_in_arr2 = sorted(list(set_arr1 - set_arr2))
missing_in_arr1 = sorted(list(set_arr2 - set_arr1))
return missing_in_arr2, missing_in_arr1
Raku Solution: Raku, being a sister language to Perl, offers similar functionality with its own syntax. Here, we also use hash tables for counting and then find the missing elements.
sub find-missing-members(@arr1, @arr2) {
my %count;
%count{$_}++ for @arr1;
%count{$_}-- for @arr2;
my @missing-in-arr2 = %count.keys.grep({ %count{$_} > 0 }).sort;
my @missing-in-arr1 = %count.keys.grep({ %count{$_} < 0 }).sort;
return @missing-in-arr2, @missing-in-arr1;
}
Task 2 - Flip Matrix
Matrix manipulation is a cornerstone in the realm of programming, offering a plethora of opportunities for efficient data handling and transformation. Today, we're tackling an intriguing challenge: flipping a binary matrix. This task involves reversing each row of a given n×nn×n binary matrix and then inverting each element. Let's explore how this can be elegantly achieved across three powerful languages: Perl, Python, and Raku.
Perl:
Perl, known for its text manipulation prowess, offers a straightforward approach to this problem. The key functions here are map
for iterating over the matrix and reverse
for flipping each row. The element inversion is simply 1 - $_
, leveraging Perl's expressive arithmetic operations.
sub flip_matrix {
my @matrix = @_;
return map { [ map { 1 - $_ } reverse @$_ ] } @matrix;
}
In the test cases, we employ is_deeply
from Test::More
to verify our results, ensuring that our function behaves as expected with various inputs.
Python:
Python's approach is characterized by its readability and succinctness, primarily using list comprehensions. The reversed
function comes in handy for row reversal, and element inversion remains a simple arithmetic operation.
def flip_matrix(matrix):
return [[1 - element for element in reversed(row)] for row in matrix]
Python's built-in unittest
framework is used for testing, providing a structured way to assert the correctness of our implementation.
Raku:
Raku, formerly known as Perl 6, builds on Perl's strengths with more expressive syntax. The solution in Raku is quite similar to Perl's, utilizing map
and reverse
, but with a more streamlined and expressive syntax.
sub flip-matrix(@matrix) {
return @matrix.map({ [($_.reverse).map({1 - $_})] });
}
Raku's testing is elegantly handled using its native Test
module, which seamlessly integrates with its powerful language features.
The Algorithm: A Closer Look
At its core, the algorithm in all three languages follows a two-step process:
- Row Reversal: Each row of the matrix is reversed. In Perl and Raku, this is done using
reverse
, while Python usesreversed
. - Element Inversion: Each binary element (0 or 1) is flipped. This is achieved through a simple arithmetic operation:
1 - element
.
This problem is a classic example of how a seemingly complex task can be broken down into simpler sub-problems, each elegantly solvable with high-level language features.