THE WEEKLY CHALLENGE - 240

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

Table of Contents

Task 1 - Acronym

Problem Statement

You are given two arrays of strings and a check string. Write a script to find out if the check string is the acronym of the words in the given array.

Solution Explanation

To find out if the check string is an acronym for the words in the given arrays, we can simply take the first character of each string in the arrays, convert it to lowercase, and concatenate them. Then we can compare this concatenated string with the check string.

Here is how we can do it in Perl:

use Test::More;

sub is_acronym {
    my ($str_ref, $chk) = @_;
    my $acronym = join '', map { lc(substr($_, 0, 1)) } @$str_ref;
    return $acronym eq $chk;
}

is(is_acronym(["Perl", "Python", "Pascal"], "ppp"), 1, 'Test Case 1');
is(is_acronym(["Perl", "Raku"], "rp"), 0, 'Test Case 2');
is(is_acronym(["Oracle", "Awk", "C"], "oac"), 1, 'Test Case 3');

done_testing;

In Raku:

use Test;

sub is-acronym(@str, $chk) {
    my $acronym = @str.map({ .substr(0, 1).lc }).join('');
    return $acronym eq $chk;
}

plan 3;

is(is-acronym(["Perl", "Python", "Pascal"], "ppp"), True, 'Test Case 1');
is(is-acronym(["Perl", "Raku"], "rp"), False, 'Test Case 2');
is(is-acronym(["Oracle", "Awk", "C"], "oac"), True, 'Test Case 3');

And in Python:

def is_acronym(chk: str, words: list[str]) -> bool:
    acronym = ''.join(word[0].lower() for word in words)
    return acronym == chk.lower()

class TestIsAcronym(unittest.TestCase):
    def test_cases(self):
        self.assertEqual(is_acronym("ppp", ["Perl", "Python", "Pascal"]), True)
        self.assertEqual(is_acronym("rp", ["Perl", "Raku"]), False)
        self.assertEqual(is_acronym("oac", ["Oracle", "Awk", "C"]), True)

Task 2 - Build Array

Problem Statement

You are given an array of integers. Write a script to create an array such that new[i] = old[old[i]] where ( 0 <= new.length ).

Solution Explanation

To solve this task, we simply iterate through the given array and fill a new array using the formula ( new[i] = old[old[i]] ).

Here is how we can do it in Perl:

sub create_new_array {
    my ($old_ref) = @_;
    my @new;
    for my $i (0..$#$old_ref) {
        push @new, $old_ref->[$old_ref->[$i]];
    }
    return \@new;
}

is_deeply(create_new_array([0, 2, 1, 5, 3, 4]), [0, 1, 2, 4, 5, 3], 'Test Case 1');
is_deeply(create_new_array([5, 0, 1, 2, 3, 4]), [4, 5, 0, 1, 2, 3], 'Test Case 2');

in Raku:

sub create-new-array(@old) {
    my @new;
    for 0..^@old.elems -> $i {
        @new.push(@old[@old[$i]]);
    }
    return @new;
}

plan 2;

is-deeply create-new-array([0, 2, 1, 5, 3, 4]), [0, 1, 2, 4, 5, 3], 'Test Case 1';
is-deeply create-new-array([5, 0, 1, 2, 3, 4]), [4, 5, 0, 1, 2, 3], 'Test Case 2';

and in Python:

def create_new_array(old: List[int]) -> List[int]:
    return [old[old[i]] for i in range(len(old))]

class TestCreateNewArray(unittest.TestCase):
    def test_cases(self):
        self.assertEqual(create_new_array([0, 2, 1, 5, 3, 4]), [0, 1, 2, 4, 5, 3])
        self.assertEqual(create_new_array([5, 0, 1, 2, 3, 4]), [4, 5, 0, 1, 2, 3])

That's it for this week! Both problems were quite straightforward but required attention to detail. Stay tuned for more Perl and Raku challenges!