Perl Weekly Challenge 289.

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

Task 1: Third Maximum

Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints.

Write a script to find the third distinct maximum in the given array.
If third maximum doesn’t exist then return the maximum number.

Example 1
Input: @ints = (5, 6, 4, 1)
Output: 4

The first distinct maximum is 6.
The second distinct maximum is 5.
The third distinct maximum is 4.
Example 2
Input: @ints = (4, 5)
Output: 5

In the given array, the third maximum doesn't exist therefore returns the maximum.
Example 3
Input: @ints =  (1, 2, 2, 3)
Output: 1

The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.

I can use a hash to obtain the distinct elements, sort them in descending order and take the third if it exists, or else, the first. This yields a simple one-liner.

Example 1:

perl -E '
++$c{$_} for @ARGV; @s=sort {$b cmp $a} keys %c; say "@ARGV -> ", $s[2]//$s[0];
' 5 6 4 1

Results:

5 6 4 1 -> 4

Example 2:

perl -E '
++$c{$_} for @ARGV; @s=sort {$b cmp $a} keys %c; say "@ARGV -> ", $s[2]//$s[0];
' 4 5

Results:

4 5 -> 5

Example 3:

perl -E '
++$c{$_} for @ARGV; @s=sort {$b cmp $a} keys %c; say "@ARGV -> ", $s[2]//$s[0];
' 1 2 2 3

Results:

1 2 2 3 -> 1

The full code is:

 1  # Perl weekly challenge 289
 2  # Task 1:  Third Maximum
 3  #
 4  # See https://wlmb.github.io/2024/09/29/PWC289/#task-1-third-maximum
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 N1 N2...
 8      to obtain the third maximum of the sequence of numbers N1 N2...
 9      FIN
10  my %count;
11  ++$count{$_} for @ARGV;
12  my @sorted=sort {$b cmp $a} keys %count;
13  say "@ARGV -> ", $sorted[2]//$sorted[0];

Examples:

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

Results:

5 6 4 1 -> 4
4 5 -> 5
1 2 2 3 -> 1

Task 2: Jumbled Letters

Submitted by: Ryan Thompson
An Internet legend dating back to at least 2001 goes something like this:

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht
oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and
lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll
raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter
by istlef, but the wrod as a wlohe.

This supposed Cambridge research is unfortunately an urban legend. However, the
effect has been studied. For example—and with a title that probably made the journal’s
editor a little nervous—Raeding wrods with jubmled lettres: there is a cost by Rayner,
White, et. al. looked at reading speed and comprehension of jumbled text.

Your task is to write a program that takes English text as its input and outputs a
jumbled version as follows:

The first and last letter of every word must stay the same
The remaining letters in the word are scrambled in a random order (if that happens
to be the original order, that is OK).
Whitespace, punctuation, and capitalization must stay the same
The order of words does not change, only the letters inside the word
So, for example, “Perl” could become “Prel”, or stay as “Perl,” but it could not become
“Pelr” or “lreP”.

I don’t know if this effect has been studied in other languages besides English, but
please consider sharing your results if you try!

I can use a regular expression to match sequences of word characters and scramble the middle ones. This leads to a two liner. The input is given in STDIN. I use the /e modifier in the substitution to allow a Perl expression in the replacement part, which I use to call a randomizer function. I also use the /m modifier to allow for newlines and the /g modifier so that I can replace all words with one substitution.

Example:

perl -n -E '
BEGIN{$/=undef;sub r($s){my $x;$x.=substr $s,(rand length $s),1,"" while length $s; $x}}
$o=$_; s/(\w)(\w*)(\w)/$1.(r $2).$3/mge; say "$o->\n$_";
' <<FIN
Your task is to write a program that takes English text
as its input and outputs a jumbled version as follows:
The first and last letter of every word must stay the same
The remaining letters in the word are scrambled in a random
order (if that happens to be the original order, that is OK).
FIN

Results:

Your task is to write a program that takes English text
as its input and outputs a jumbled version as follows:
The first and last letter of every word must stay the same
The remaining letters in the word are scrambled in a random
order (if that happens to be the original order, that is OK).
->
Your tsak is to write a parrogm taht takes Egsnlih txet
as its iunpt and ottuups a jmubled voersin as foollws:
The frsit and last ltteer of erevy word must stay the same
The rmneniiag letrets in the wrod are sbacrmeld in a rnadom
oredr (if taht hpaneps to be the orngiial oerdr, that is OK).

The full code is similar.

 1  # Perl weekly challenge 289
 2  # Task 2:  Jumbled Letters
 3  #
 4  # See https://wlmb.github.io/2024/09/29/PWC289/#task-2-jumbled-letters
 5  use v5.36;
 6  $/=undef; # slurp
 7  sub randomize($string){
 8      my $randomized="";
 9      $randomized.=substr $string, (rand length $string), 1, "" while length $string;
10      $randomized
11  }
12  for(<>){
13      my $original=$_;
14      s/(\w)(\w*)(\w)/$1.(randomize $2).$3/mge;
15      say "$original->\n$_";
16  }

Example:

./ch-2.pl <<FIN
Your task is to write a program that takes English text
as its input and outputs a jumbled version as follows:
The first and last letter of every word must stay the same
The remaining letters in the word are scrambled in a random
order (if that happens to be the original order, that is OK).
FIN

Results:

Your task is to write a program that takes English text
as its input and outputs a jumbled version as follows:
The first and last letter of every word must stay the same
The remaining letters in the word are scrambled in a random
order (if that happens to be the original order, that is OK).
->
Yuor task is to witre a pgarorm that tkeas English txet
as its inupt and outtups a jbemlud vieosrn as folwlos:
The fisrt and last lteetr of eervy wrod must stay the smae
The riaenming lerttes in the word are sbcremald in a rnoadm
oedrr (if that hppneas to be the oraiingl oerdr, taht is OK).

/;

Written on September 29, 2024