Perl Weekly Challenge 319.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 319.
Task 1: Word Count
Submitted by: Mohammad Sajid Anwar
You are given a list of words containing alphabetic characters only.
Write a script to return the count of words either starting with a
vowel or ending with a vowel.
Example 1
Input: @list = ("unicode", "xml", "raku", "perl")
Output: 2
The words are "unicode" and "raku".
Example 2
Input: @list = ("the", "weekly", "challenge")
Output: 2
Example 3
Input: @list = ("perl", "python", "postgres")
Output: 0
I use a regular expression to filter the list, and then convert the list to a scalar to get the output. This yields a half-liner.
Example 1:
perl -E '
$v="[aeiou]"; say "@ARGV -> ", 0+grep{/(^$v)|($v$)/}@ARGV
' unicode xml raku perl
Results:
unicode xml raku perl -> 2
Example 2:
perl -E '
$v="[aeiou]"; say "@ARGV -> ", 0+grep{/(^$v)|($v$)/}@ARGV
' the weekly challenge
Results:
the weekly challenge -> 2
Example 3:
perl -E '
$v="[aeiou]"; say "@ARGV -> ", 0+grep{/(^$v)|($v$)/}@ARGV
' perl python postgres
Results:
perl python postgres -> 0
Notice that I assumed a e i o u
are the only vowels. Thus, the
program would fail in other languages, such as Spanish, that have
respectable accented vowels.
The full code is similar
1 # Perl weekly challenge 319
2 # Task 1: Word Count
3 #
4 # See https://wlmb.github.io/2025/04/28/PWC319/#task-1-word-count
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 W1 W2...
8 to count how many of the words Wn start or end in a vowel.
9 FIN
10 my $vowels="[aeiou]";
11 my $re=qr/(^$vowels)|($vowels$)/;
12 my $result = 0 + grep{/$re/} @ARGV;
13 say "@ARGV -> $result";
Example:
./ch-1.pl unicode xml raku perl
./ch-1.pl the weekly challenge
./ch-1.pl perl python postgres
Results:
unicode xml raku perl -> 2
the weekly challenge -> 2
perl python postgres -> 0
Task 2: Minimum Common
Submitted by: Mohammad Sajid Anwar
You are given two arrays of integers.
Write a script to return the minimum integer common to both arrays.
If none found return -1.
Example 1
Input: @array_1 = (1, 2, 3, 4)
@array_2 = (3, 4, 5, 6)
Output: 3
The common integer in both arrays: 3, 4
The minimum is 3.
Example 2
Input: @array_1 = (1, 2, 3)
@array_2 = (2, 4)
Output: 2
Example 3
Input: @array_1 = (1, 2, 3, 4)
@array_2 = (5, 6, 7, 8)
Output: -1
I use a hash with the elements of one array as keys, to filter the second array and find the minimum of the surviving elements, which belong to both arrays. This yields a one-liner.
Example 1:
perl -MList::Util=min -E '
($x,$y)=map{[split " ",$_]}@ARGV;$w{$_}++for@$x;$r=(min grep{$w{$_}}@$y)//-1;say join("; ", @ARGV)," -> $r";
' "1 2 3 4" "3 4 5 6"
Results:
1 2 3 4; 3 4 5 6 -> 3
Example 2:
perl -MList::Util=min -E '
($x,$y)=map{[split " ",$_]}@ARGV;$w{$_}++for@$x;$r=(min grep{$w{$_}}@$y)//-1;say join("; ", @ARGV)," -> $r";
' "1 2 3" "2 4"
Results:
1 2 3; 2 4 -> 2
Example 3:
perl -MList::Util=min -E '
($x,$y)=map{[split " ",$_]}@ARGV;$w{$_}++for@$x;$r=(min grep{$w{$_}}@$y)//-1;say join("; ", @ARGV)," -> $r";
' "1 2 3 4" "5 6 7 8"
Results:
1 2 3 4; 5 6 7 8 -> -1
The full code is:
1 # Perl weekly challenge 319
2 # Task 2: Minimum Common
3 #
4 # See https://wlmb.github.io/2025/04/28/PWC319/#task-2-minimum-common
5 use v5.36;
6 use List::Util qw/min/;
7 die <<~"FIN" unless @ARGV && @ARGV%2==0;
8 Usage: $0 X1 Y1 X2 Y2...
9 where Xn and Yn are strings with space separated numbers,
10 to find the minimum common element of the corresponding sets.
11 FIN
12 for my ($x,$y)(@ARGV){
13 my ($ax, $ay)=map {[split " ", $_]} $x, $y;
14 my %found;
15 $found{$_}++ for @$ax;
16 my $result = (min grep{$found{$_}} @$ay)//-1;
17 say "$x; $y -> $result";
18 }
Examples:
./ch-2.pl "1 2 3 4" "3 4 5 6" "1 2 3" "2 4" "1 2 3 4" "5 6 7 8"
Results:
1 2 3 4; 3 4 5 6 -> 3
1 2 3; 2 4 -> 2
1 2 3 4; 5 6 7 8 -> -1
/;