RabbitFarm

2025-04-06

Finding a Third of the Words

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

Part 1: Find Words

You are given a list of words and a character. Write a script to return the index of word in the list where you find the given character.

This can be done in essentially one line. Rather than write a true Perl one-liner for the command line though, we’ll package this into a single subroutine.

Here’s our one subroutine.

find words 1 ⟩≡


sub find_words{
my($s, $c) = @_;
return grep {$s->[$_] =~ m/$c/} 0 .. @{$s} - 1;
}

Fragment referenced in 2.

Putting it all together...

"ch-1.pl" 2


preamble 3
find words 1
main 4

preamble 3 ⟩≡


use v5.40;

Fragment referenced in 2, 6.

The rest of the code just runs some simple tests.

main 4 ⟩≡


MAIN:{
say q/(/ . join(q/, /, find_words([q/the/, q/weekly/, q/challenge/], q/e/)). q/)/;
say q/(/ . join(q/, /, find_words([q/perl/, q/raku/, q/python/], q/p/)) . q/)/;
say q/(/ . join(q/, /, find_words([q/abc/, q/def/, q/bbb/, q/bcd/], q/b/)) . q/)/;
}

Fragment referenced in 2.

Sample Run
$ perl perl/ch-1.pl 
(0, 1, 2) 
(0, 2) 
(0, 2, 3)
    

Part 2: Find Third

You are given a sentence and two words. Write a script to return all words in the given sentence that appear in sequence to the given two words.

Similar to the first part this will be a single short subroutine. We’re just going to loop over the words and match as we go. There are two small things to note here: we strip out any punctuation from our sentence and the empty string q// is considered by Perl to be a false value. The latter is only important in that is how we initialize $next.

find third 5 ⟩≡


sub find_third{
my ($s, $first, $second) = @_;
$s =~ s/[[:punct:]]//g;
my @thirds = ();
my($previous, $current, $next) = (q//, q//, q//);
do{
push @thirds, $_ if $next;
$current = $_;
$next = 1 if $previous eq $first && $current eq $second;
$next = 0 unless $previous eq $first && $current eq $second;
$previous = $current;
} for split q/\s+/, $s;
return @thirds;
}

Fragment referenced in 6.

The rest of the code drives some tests.

"ch-2.pl" 6


preamble 3
find third 5
main 7

main 7 ⟩≡


MAIN:{
say q/(/ . join(q/, /, find_third(q/Perl is a my favourite language but Python is my favourite too./, q/my/, q/favourite/)). q/)/;
say q/(/ . join(q/, /, find_third(q/Barbie is a beautiful doll also also a beautiful princess./, q/a/, q/beautiful/)) . q/)/;
say q/(/ . join(q/, /, find_third(q/we will we will rock you rock you./, q/we/, q/will/)) . q/)/;
}

Fragment referenced in 6.

Sample Run
$ perl perl/ch-2.pl 
(language, too) 
(doll, princess) 
(we, rock)
    

References

The Weekly Challenge 315
Generated Code

posted at: 17:29 by: Adam Russell | path: /perl | permanent link to this entry