Perl Weekly Challenge 262.

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

Task 1: Max Positive Negative

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

Write a script to return the maximum number of either positive or
negative integers in the given array.

Example 1
Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
Output: 4

Count of positive integers: 4
Count of negative integers: 3
Maximum of count of positive and negative integers: 4
Example 2
Input: @ints = (-1, -2, -3, 1)
Output: 3

Count of positive integers: 1
Count of negative integers: 3
Maximum of count of positive and negative integers: 3
Example 3
Input: @ints = (1,2)
Output: 2

Count of positive integers: 2
Count of negative integers: 0
Maximum of count of positive and negative integers: 2

I make an array to count the sign of the numbers (negative->0, non-negative->1) and I print the maximum. I provide the numbers in @ARGV after -- to disable interpretation of the negative sign as an argument switch. This fits a half-liner:

Example 1:

perl -MList::Util=max -E '$c[$_>=0]++ for @ARGV; say "@ARGV -> ", max @c;' -- -3 1 2 -1 3 -2 4

Results:

-3 1 2 -1 3 -2 4 -> 4

Example 2:

perl -MList::Util=max -E '$c[$_>=0]++ for @ARGV; say "@ARGV -> ", max @c;' -- -1 -2 -3 1

Results:

-1 -2 -3 1 -> 3

Example 3:

perl -MList::Util=max -E '$c[$_>=0]++ for @ARGV; say "@ARGV -> ", max @c;' -- 1 2

Results:

1 2 -> 2

The full code is:

 1  # Perl weekly challenge 262
 2  # Task 1:  Max Positive Negative
 3  #
 4  # See https://wlmb.github.io/2024/03/24/PWC262/#task-1-max-positive-negative
 5  use v5.36;
 6  use List::Util qw(max);
 7  die <<~"FIN" unless @ARGV;
 8      Usage: $0 N1 [N2...]
 9      to find the maximum number of equally signed numbers N_i
10      FIN
11  my @count=(0,0);
12  $count[$_>=0//0]++ for @ARGV;
13  say "@ARGV -> ", max @count;

Example:

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

Results:

-3 1 2 -1 3 -2 4 -> 4
-1 -2 -3 1 -> 3
1 2 -> 2

Task 2: Count Equal Divisible

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

Write a script to return the number of pairs (i, j) where

a. 0 <= i < j < size of @ints
b. ints[i] == ints[j]
c. i x j is divisible by k
Example 1
Input: @ints = (3,1,2,2,2,1,3) and $k = 2
Output: 4

(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2
(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2
(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2
(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2
Example 2
Input: @ints = (1,2,3) and $k = 1
Output: 0

I read the divisor and the list of integers from @ARGV. I make a double loop through the indices of the list checking the conditions given. The results fits a 1.5liner

perl -E '($k,@x)=@ARGV;for $i(0..@x-2){$x[$i]==$x[$_] && ($i*$_)%$k==0 && ++$c for($i+1..@x-1)};
say "k=$k, ints=@x -> ", $c//0;' 2 3 1 2 2 2 1 3

Results:

k=2, ints=3 1 2 2 2 1 3 -> 4

perl -E '($k,@x)=@ARGV;for $i(0..@x-2){$x[$i]==$x[$_] && ($i*$_)%$k==0 && ++$c for($i+1..@x-1)};
say "k=$k, ints=@x -> ", $c//0;' 1 1 2 3

Results:

k=1, ints=1 2 3 -> 0

The corresponding full code is:

 1  # Perl weekly challenge 262
 2  # Task 2:  Count Equal Divisible
 3  #
 4  # See https://wlmb.github.io/2024/03/24/PWC262/#task-2-count-equal-divisible
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV>=2;
 7      Usage: $0 K N1 [N2...]
 8      to count pairs N_i==N_j where K divides i*j
 9      FIN
10  my ($k,@ints)=@ARGV;
11  my $count=0;
12  for my $i(0..@ints-2){
13      for ($i+1..@ints-1){
14  	++$count if $ints[$i]==$ints[$_] && ($i*$_)%$k==0;
15      }
16  }
17  say "k=$k, ints=@ints -> $count";

Example:

./ch-2.pl 2 3 1 2 2 2 1 3
./ch-2.pl 1 1 2 3

Results:

k=2, ints=3 1 2 2 2 1 3 -> 4
k=1, ints=1 2 3 -> 0
Written on March 24, 2024