Perl Weekly Challenge 230: Count Words

These are some answers to the Week 230, Task 2, 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 August 20, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Count Words

You are given an array of words made up of alphabetic characters and a prefix.

Write a script to return the count of words that starts with the given prefix.

Example 1

Input: @words  = ("pay", "attention", "practice", "attend")
       $prefix = "at"
Ouput: 2

Two words "attention" and "attend" starts with the given prefix "at".

Example 2

Input: @words  = ("janet", "julia", "java", "javascript")
       $prefix = "ja"
Ouput: 3

Three words "janet", "java" and "javascripr" starts with the given prefix "ja".

Again a very simple task. We just need to check each word against the prefix. To make things simpler, the first word passed as parameter to our subroutine will be the prefix.

Count Words in Raku

There are many ways to verify whether a word starts with a given prefix (index, substr, regex, etc.). We will use the built-in starts-with method, which is designed exactly for this purpose and is therefore very simple to use and likely to be efficient. We do it in a grep routine and count the input items that match the prefix.

sub count-words (@in is copy) {
    my $prefix = shift @in;
    return (grep { .starts-with($prefix) }, @in).elems;
}

for <at pay attention practice attend>, 
    <ja janet julia java javascript> -> @test {
    printf "%-3s - %-30s => ", 
        @test[0], "@test[1..@test.end]";
    say count-words @test;
}

This program displays the following output:

$ raku ./count-words.raku
at  - pay attention practice attend  => 2
ja  - janet julia java javascript    => 3

Count Words in Perl

This Perl program is based on the same approach as the above Raku program, but it uses the built-in substr function.

use strict;
use warnings;
use feature qw /say/;

sub count_words {
    my $prefix = shift;
    return scalar grep { substr($_, 0, length $prefix)
        eq $prefix } @_;
}
for my $test ( [<at pay attention practice attend>], 
               [<ja janet julia java javascript>] ) {
    my @t = @$test;
    printf "%-3s - %-30s => ", 
        $t[0], "@t[1..$#t]";
    say count_words @t;
}

This program displays the following output:

$ perl ./count-words.pl
at  - pay attention practice attend  => 2
ja  - janet julia java javascript    => 3

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 August 27, 2023. 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.