Perl Weekly Challenge 225: Max Words

These are some answers to the Week 225, 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 July 16, 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 1: Max Words

You are given a list of sentences, @list.

A sentence is a list of words that are separated by a single space with no leading or trailing spaces.

Write a script to find out the maximum number of words that appear in a single sentence.

Example 1

Input: @list = ("Perl and Raku belong to the same family.",
                "I love Perl.",
                "The Perl and Raku Conference.")
Output: 8

Example 2

Input: @list = ("The Weekly Challenge.",
                "Python is the most popular guest language.",
                "Team PWC has over 300 members.")
Output: 7

We will suppose that the point is just to count the words in a sentence, not to try to count unique words (i.e. without duplicates). If we wanted unique words, it would be very easy to change it (for example by adding an invocation to the unique method in the Raku version).

Max Words in Raku

In Raku, we simply chain invocations of the words and elems methods.

sub max-words (@sentences) {
    my $max = 0;
    for @sentences -> $sentence {
        my $cw = $sentence.words.elems;
        $max = $cw if $cw > $max;
    }
    return $max;
}

my @tests = 
    ("The quick brown fox jumps over the lazy dog",
        "Lorem ipsum dolor sit amet"),
    ("Perl and Raku belong to the same family.",
        "I love Perl.",
        "The Perl and Raku Conference."),
    ("The Weekly Challenge.",
        "Python is the most popular guest language.",
        "Team PWC has over 300 members.");
for @tests -> @test {
    say max-words @test;
}

This program displays the following output:

$ raku ./max-words.raku
9
8
7

Max Words in Perl

This is a port to Perl of the Raku program above. Here, we use split to divide the sentence into a list of words and use scalar to get the word count.

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

sub max_words {
    my $max = 0;
    for my $sentence (@_) {
        my $cw = scalar split /\s+/, $sentence;
        $max = $cw if $cw > $max;
    }
    return $max;
}

my @tests = (
    ["The quick brown fox jumps over the lazy dog",
        "Lorem ipsum dolor sit amet"], 
    ["Perl and Raku belong to the same family.",
        "I love Perl.",
        "The Perl and Raku Conference."],
    ["The Weekly Challenge.",
        "Python is the most popular guest language.",
        "Team PWC has over 300 members."]);
for my $test (@tests) {
    say max_words @$test;
}

This program displays the following output:

$ perl ./max-words.pl
9
8
7

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 July 23, 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.