Perl Weekly Challenge 256: Maximum Pairs

These are some answers to the Week 256, Task 1, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on February 18, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Maximum Pairs

You are given an array of distinct words, @words.

Write a script to find the maximum pairs in the given array. The words $words[i] and $words[j] can be a pair one is reverse of the other.

Example 1

Input: @words = ("ab", "de", "ed", "bc")
Output: 1

There is one pair in the given array: "de" and "ed"

Example 2

Input: @words = ("aa", "ba", "cd", "ed")
Output: 0

Example 3

Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))
Output: 2

Maximum Pairs in Raku

We just run two nested loops on the input array and increment a counter whenever one word is ther reverse of another one. The Raku routine for reversing a word is flip.

sub find-pairs (@in) {
    my $nb-pairs = 0;
    for 0..@in.end -> $i {
        for $i^..@in.end -> $j {
            $nb-pairs++ if @in[$i] eq @in[$j].flip;
        }
    }
    return $nb-pairs;
}

my @tests = <ab de ed bc>, <aa ba cd ed>, <uv qp st vu mn pq> ;
for @tests -> @test {
    printf "%-20s => ", "@test[]";
    say find-pairs @test;
}

This program displays the following output:

$ raku ./find-pairs.raku
ab de ed bc          => 1
aa ba cd ed          => 0
uv qp st vu mn pq    => 2

Maximum Pairs in Perl

This is a port to Perl of the above Raku program, with also two nested loops.

use strict;
use warnings;
use feature 'say';

sub find_pairs {
    my @in = @_;
    my $nb_pairs = 0;
    for my $i (0..$#in) {
        for my $j ($i + 1 ..$#in) {
            $nb_pairs++ if $in[$i] eq reverse $in[$j];
        }
    }
    return $nb_pairs;
}

my @tests = ([<ab de ed bc>], [<aa ba cd ed>],
             [<uv qp st vu mn pq>]);
for my $test (@tests) {
    printf "%-20s => ", "@$test";
    say find_pairs @$test;
}

This program displays the following output:

$ perl ./find-pairs.pl
ab de ed bc          => 1
aa ba cd ed          => 0
uv qp st vu mn pq    => 2

Maximum Pairs in Julia

This is a port to Julia of the above Raku program, with also two nested loops. Remember that Julia array indexes start at 1, not 0.

using Printf

function find_pairs(in)
    nb_pairs = 0
    for i in 1:size(in, 1)
        for j in i+1:size(in, 1)
            if in[i] == reverse(in[j])
                nb_pairs += 1
            end
        end
    end
    return nb_pairs
end

tests = [ ["ab", "de", "ed", "bc"],
          ["aa", "ba", "cd", "ed"],
          ["uv", "qp", "st", "vu", "mn", "pq"] ]

for test in tests
    @printf "%-40s => " "$test"
    println(find_pairs(test))
end

This program displays the following output:

$  julia ./find-pairs.jl
["ab", "de", "ed", "bc"]                 => 1
["aa", "ba", "cd", "ed"]                 => 0
["uv", "qp", "st", "vu", "mn", "pq"]     => 2

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on February 25, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.