Perl Weekly Challenge 207.

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

Task 1: Keyboard Word

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

Write a script to print all the words in the given array that can be types
using alphabet on only one row of the keyboard.

Let us assume the keys are arranged as below:

Row 1: qwertyuiop
Row 2: asdfghjkl
Row 3: zxcvbnm
Example 1
Input: @words = ("Hello","Alaska","Dad","Peace")
Output: ("Alaska","Dad")
Example 2
Input: @array = ("OMG","Bye")
Output: ()

To solve task 1 I just have to filter all words to check the correspond to one of the rows. A single regular expression should be enough. I use an alternation between the three rows and disregard case:

perl -E 'say join " ", @ARGV, "->", grep {/^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$/i} @ARGV' Hello Alaska Dad Peace
perl -E 'say join " ", @ARGV, "->", grep {/^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$/i} @ARGV' OMG Bye

Results:

Hello Alaska Dad Peace -> Alaska Dad
OMG Bye ->

Almost no changes for the full code.

 1  # Perl weekly challenge 207
 2  # Task 1:  Keyboard Word
 3  #
 4  # See https://wlmb.github.io/2023/03/06/PWC207/#task-1-keyboard-word
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 W1 [W2...]
 8      to find which of the words W1 W2... may be written using a single keyboard row.
 9      FIN
10  say join " ", @ARGV, "->",
11      grep {/^([qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$/i} @ARGV;

Examples:

./ch-1.pl Hello Alaska Dad Peace
./ch-1.pl OMG Bye

Results:

Hello Alaska Dad Peace -> Alaska Dad
OMG Bye ->

Task 2: H-Index

Submitted by: Mohammad S Anwar
You are given an array of integers containing citations a researcher has received
for each paper.

Write a script to compute the researcher’s H-Index. For more information please
checkout the wikipedia page.

The H-Index is the largest number h such that h articles have at least h citations
each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations
(ordered from greatest to least), then the author’s h-index is 3, because the author
has three publications with 3 or more citations. However, the author does not have
four publications with 4 or more citations.


Example 1
Input: @citations = (10,8,5,4,3)
Output: 4

Because the 4th publication has 4 citations and the 5th has only 3.
Example 2
Input: @citations = (25,8,5,3,3)
Output: 3

The H-Index is 3 because the fourth paper has only 3 citations.

I order the articles in descending citation order and increase a counter until it becomes larger

perl -E '$h=0; for(sort{$b<=>$a}@ARGV){last if $_<=$h; ++$h} say join " ", @ARGV, "->", $h;' 10 8 5 4 3
perl -E '$h=0; for(sort{$b<=>$a}@ARGV){last if $_<=$h; ++$h} say join " ", @ARGV, "->", $h;' 25 8 5 3 3

Results:

10 8 5 4 3 -> 4
25 8 5 3 3 -> 3

The full code is almost identical.

 1  # Perl weekly challenge 207
 2  # Task 2:  H-Index
 3  #
 4  # See https://wlmb.github.io/2023/03/06/PWC207/#task-2-h-index
 5  use v5.36;
 6  my $h=0;
 7  for(sort{$b<=>$a}@ARGV){
 8      last if $_<=$h;
 9      ++$h
10  }
11  say join " ", @ARGV, "->", $h;

Examples:

./ch-2.pl 10 8 5 4 3
./ch-2.pl 25 8 5 3 3

Results:

10 8 5 4 3 -> 4
25 8 5 3 3 -> 3
Written on March 6, 2023