Perl Weekly Challenge 230.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 230.

Task 1: Separate Digits

Submitted by: Mohammad S Anwar
You are given an array of positive integers.

Write a script to separate the given array into single digits.

Example 1
Input: @ints = (1, 34, 5, 6)
Output: (1, 3, 4, 5, 6)
Example 2
Input: @ints = (1, 24, 51, 60)
Output: (1, 2, 4, 5, 1, 6, 0)

Assuming the input is correct, I just have to split it into characters.

Example 1:

perl -E 'say join " ", @ARGV, " -> ", map {split ""} @ARGV' 1 34 5 6

Results:

1 34 5 6  ->  1 3 4 5 6

Example 2:

perl -E 'say join " ", @ARGV, " -> ", map {split ""} @ARGV' 1 24 51 60

Results:

1 24 51 50  ->  1 2 4 5 1 5 0

Full code:

 1  # Perl weekly challenge 230
 2  # Task 1:  Separate Digits
 3  #
 4  # See https://wlmb.github.io/2023/08/14/PWC230/#task-1-separate-digits
 5  use v5.36;
 6  use List::Util qw(all);
 7  die <<~"FIN" unless @ARGV;
 8     Usage: $0 N1 [N2..]
 9     to separate the positive integers N1 N2... into their decimal digits.
10     FIN
11  for(@ARGV){ # Check input
12      die "Expected only positive integers: $_" unless /^\d+$/;
13  }
14  say join " ", @ARGV, " -> ", map {split ""} @ARGV;

Examples:

./ch-1.pl 1 34 5 6
./ch-1.pl 1 24 51 60

Results:

1 34 5 6  ->  1 3 4 5 6
1 24 51 60  ->  1 2 4 5 1 6 0

Results:

1 34 5 6  ->  1 3 4 5 6 a
1 24 51 60  ->  1 2 4 5 1 6 0

Task 2: Count Words

Submitted by: Mohammad S Anwar
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".

I assume the prefix and the words are given in @ARGV. I further assume that uppercase is distinct from lowercase. Then, I can use the prefix to build a regular expression with which I filter the words and count the number of elements.

Example 1:

perl -E '($p,@w)=@ARGV; say "Prefix: $p, Words: @w, Count: ", 0+grep{/^$p/}@w
' at pay attention practice attend

Results:

Prefix: at, Words: pay attention practice attend, Count: 2

Example 2:

perl -E '($p,@w)=@ARGV; say "Prefix: $p, Words: @w, Count: ", 0+grep{/^$p/}@w
' ja janet julia java javascript

Results:

Prefix: ja, Words: janet julia java javascript, Count: 3

Notice that Perl has some curious behaviour sometimes when converting some collections into scalars, as illustrated below:

perl -E 'say "0+(0,1,2)->", 0+(0,1,2); say "0+(0..2)->", 0+(0..2);
     say "0+map{\$_}(0..2)->", 0+map{$_}(0..2);'

Results:

0+(0,1,2)->2
0+(0..2)->1
0+map{$_}(0..2)->3

So it is a good idea to check if your conversion of collections to a scalar does what is expected. Actually, I can’t figure now the second example nor the following:

perl -E 'say "0+1..3->", 0+1..3; say "1+1..3->", 1+1..3;
     say "2+1..3->", 2+1..3; say "3+1..3->", 3+1..3;'

Results:

0+1..3->123
1+1..3->23
2+1..3->3
3+1..3->

The full code is:

 1  # Perl weekly challenge 230
 2  # Task 2:  Count Words
 3  #
 4  # See https://wlmb.github.io/2023/08/14/PWC230/#task-2-count-words
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV>=2;
 7      Usage: $0 prefix W1 [W2...]
 8      to count how many of the words W1, W2... start with the prefix.
 9      FIN
10  my ($prefix ,@words)=@ARGV;
11  say "Prefix: $prefix, Words: @words, Count: ", 0+grep {/^$prefix/} @words;

Examples:

./ch-2.pl at pay attention practice attend
./ch-2.pl ja janet julia java javascript

Results:

Prefix: at, Words: pay attention practice attend, Count: 2
Prefix: ja, Words: janet julia java javascript, Count: 3

Notice that the program allows a regular expression as prefix, so it could be used as follows:

./ch-2.pl "at|ja" pay attention practice attend janet julia java javascript

Results:

Prefix: at|ja, Words: pay attention practice attend janet julia java javascript, Count: 5
Written on August 14, 2023