Counting words and
subtracting numbers
Weekly challenge 225 — 10 July 2023
Week 225: 10 Jul 2023
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
On the face of it, this is quite a simple challenge. A loop over sentences enclosing a loop counting the words is easy to code, maybe even in a single line.
I took a slightly different approach. I think an explicit outer loop over sentences
is sensible, but I then counted the words in each sentence using a feature I'm not
sure I've ever used before, which is that the s|||g
construct returns the number of substitutions
made if it's called in scalar context. So
$words = ($sentence =~ s|[^\s]+||g)
returns the number of words in $sentence
- because it deletes all the strings
of consecutive non-blanks - ie words.
But there is a catch! In doing so, $sentence is modified, and you can't modify a loop variable, so the outer loop can't be the obvious
for $sentence (@_)
but needs to be, for example
while ($sentence = $_[$j++])
.
I wondered if
$words = ($sentence =~ m|[^\s]+|g)
would work for counting the words, but it only returns 1 (for any number of matches) or 0 (for none).
#!/usr/bin/perl use v5.16; # The Weekly Challenge - 2023-07-10 use utf8; # Week 225 task 1 - Max words use strict; # Peter Campbell Smith use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge binmode(STDOUT, ':utf8'); max_words('Perl and Raku belong to the same family.', 'I love Perl.', 'The Perl and Raku Conference.'); max_words('The Weekly Challenge.', 'Python is the most popular guest language.', 'Team PWC has over 300 members.'); max_words('Our solo sponsor Pete Sergeant has been a great support to keep us motivated.', 'We are lucky that he agreed to continue the journey with us in the year 2023.', 'I would like to personally thank Pete and his entire team for their generosity.', 'It would be great if we could add few more to sponsor the prize money so that we could go back and declare weekly champions as we have done in the past.', 'I hope and wish this will become possible in 2023.', 'The amount doesn’t have to be huge.', 'However, it would be nice to show off bunch of supporters.', 'If an organisation comes forward and supports us then that would be the ultimate achievement.'); sub max_words { my ($max, $sentence, $words, $rubric, $j); # loop over sentences $max = 0; while ($sentence = $_[$j++]) { $rubric .= qq['$sentence',\n ]; # in scalar context s||| returns the number of substitutions made $words = ($sentence =~ s|[^\s]+||g); $max = $words if $words > $max; } # show the results $rubric =~ s|,\n +$||; say qq[\nInput: \@list = ($rubric)]; say qq[Output: $max]; }
Input: @list = ('Perl and Raku belong to the same family.', 'I love Perl.', 'The Perl and Raku Conference.') Output: 8 Input: @list = ('The Weekly Challenge.', 'Python is the most popular guest language.', 'Team PWC has over 300 members.') Output: 7 Input: @list = ('Our solo sponsor Pete Sergeant has been a great support to keep us motivated.', 'We are lucky that he agreed to continue the journey with us in the year 2023.', 'I would like to personally thank Pete and his entire team for their generosity.', 'It would be great if we could add few more to sponsor the prize money so that we could go back and declare weekly champions as we have done in the past.', 'I hope and wish this will become possible in 2023.', 'The amount doesn’t have to be huge.', 'However, it would be nice to show off bunch of supporters.', 'If an organisation comes forward and supports us then that would be the ultimate achievement.') Output: 32
Any content of this website which has been created by Peter Campbell Smith is in the public domain