# Perl Weekly Challenge 240.

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

You are given an array of strings and a check string.

Write a script to find out if the check string is the acronym of the words in the given array.

Example 1
Input: @str = ("Perl", "Python", "Pascal")
\$chk = "ppp"
Output: true
Example 2
Input: @str = ("Perl", "Raku")
\$chk = "rp"
Output: false
Example 3
Input: @str = ("Oracle", "Awk", "C")
\$chk = "oac"
Output: true

From the example, the array is assumed ordered. So I only have to join the first letter of each word in order to build the acronym. I can normalize it to lowercase. I assume the acronym is the first element of @ARGC and the other elements form the array. The result is a oneliner.

Example 1:

perl -E '
\$x=shift; say "\$x <= @ARGV: ", lc \$x eq (lc join "", map {substr \$_, 0,1} @ARGV)?"True":"False"
' ppp Perl Python Pascal

Results:

ppp <= Perl Python Pascal: True

Example 2:

perl -E '
\$x=shift; say "\$x <= @ARGV: ", lc \$x eq (lc join "", map {substr \$_, 0,1} @ARGV)?"True":"False"
' rp Perl Raku

Results:

rp <= Perl Raku: False

Example 3:

perl -E '
\$x=shift; say "\$x <= @ARGV: ", lc \$x eq (lc join "", map {substr \$_, 0,1} @ARGV)?"True":"False"
' oac Oracle Awk C

Results:

oac <= Oracle Awk C: True

The full code is similar:

1  # Perl weekly challenge 240
3  #
5  use v5.36;
6  die <<~"FIN" unless @ARGV;
7      Usage: \$0 A W1 [W2...]
8      to test if A is an acronym of the words W1, W2...
9      FIN
10  my \$acronym=shift;
11  say "\$acronym <= @ARGV: ", lc \$acronym eq (lc join "", map {substr \$_, 0,1} @ARGV)?"True":"False"

Examples:

./ch-1.pl ppp Perl Python Pascal
./ch-1.pl rp Perl Raku
./ch-1.pl oac Oracle Awk C

Results:

ppp <= Perl Python Pascal: True
rp <= Perl Raku: False
oac <= Oracle Awk C: True

You are given an array of integers.

Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length.

Example 1
Input: @int = (0, 2, 1, 5, 3, 4)
Output: (0, 1, 2, 4, 5, 3)
Example 2
Input: @int = (5, 0, 1, 2, 3, 4)
Output: (4, 5, 0, 1, 2, 3)

Perl allows indexing an array by an array to produce an array. This yields an extremely compact solution.

Example 1:

perl -E '@x=@ARGV; say "@x -> @x[@x]"' 0 2 1 5 3 4

Results:

0 2 1 5 3 4 -> 0 1 2 4 5 3

(Choroba got this solution first!)

Example 2:

perl -E '@x=@ARGV; say "@x -> @x[@x]"' 5 0 1 2 3 4

Results:

5 0 1 2 3 4 -> 4 5 0 1 2 3

Interestingly, the operation may be iterated, yielding fixed points or periodic orbits:

perl -E '@x=@ARGV; print("@x ->"), @x=@x[@x] for 1..6; say "..."' 0 2 1 5 3 4

Results:

0 2 1 5 3 4 ->0 1 2 4 5 3 ->0 1 2 5 3 4 ->0 1 2 4 5 3 ->0 1 2 5 3 4 ->0 1 2 4 5 3 ->...

The full code is almost identical, with a couple of tests.

1  # Perl weekly challenge 240
2  # Task 2:  Build Array
3  #
5  use v5.36;
6  use List::Util qw(min max);
7  die <<~"FIN" unless @ARGV;
8      Usage: \$0 N0 [N1...]
9      to iterate indexing the array N0, N1...
10      FIN
11  die "Elements should be >=0 and < #of elements" unless 0<=min @ARGV && max @ARGV<@ARGV;
12  say "@ARGV -> @ARGV[@ARGV]";

Examples:

./ch-2.pl 0 2 1 5 3 4
./ch-2.pl 5 0 1 2 3 4

Results:

0 2 1 5 3 4 -> 0 1 2 4 5 3
5 0 1 2 3 4 -> 4 5 0 1 2 3

/;

Written on October 23, 2023