Perl Weekly Challenge 320.

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

Task 1: Maximum Count

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

Write a script to return the maximum between the number of positive
and negative integers. Zero is neither positive nor negative.


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

There are 3 positive integers.
There are 3 negative integers.
The maximum between 3 and 3 is 3.

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

There are 1 positive integers.
There are 2 negative integers.
The maximum between 2 and 1 is 2.

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

There are 4 positive integers.
There are 0 negative integers.
The maximum between 4 and 0 is 4.

I striaghtforwardly count the positive and negative integers and take the maximum of the counts. This yields a half-liner.

Example 1:

perl -MList::Util=max -E '
for(@ARGV){$_>0&&++$p;$_<0&&++$n;} say "@ARGV -> ",max($n,$p);
' - -3 -2 -1 1 2 3

Results:

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

I added the extra - so that Perl doesn’t interpret negative arguments as options to Perl.

Example 2:

perl -MList::Util=max -E '
for(@ARGV){$_>0&&++$p;$_<0&&++$n;} say "@ARGV -> ",max($n,$p);
' - -2 -1 0 0 1

Results:

- -2 -1 0 0 1 -> 2

Example 3:

perl -MList::Util=max -E '
for(@ARGV){$_>0&&++$p;$_<0&&++$n;} say "@ARGV -> ",max($n,$p);
' 1 2 3 4

Results:

1 2 3 4 -> 4

The full code is similar.

 1  # Perl weekly challenge 320
 2  # Task 1:  Maximum Count
 3  #
 4  # See https://wlmb.github.io/2025/05/05/PWC320/#task-1-maximum-count
 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 between the count of positive
10      and negative numbers among N1 N2...
11      FIN
12  my ($positive, $negative)=(0, 0);
13  for(@ARGV){
14      ++$positive if $_>0;
15      ++$negative if $_<0;
16  }
17  say "@ARGV -> ",max($negative, $positive);

Example:

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

Results:

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

Task 2: Sum Difference

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

Write a script to return the absolute difference between digit
sum and element sum of the given array.


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

Element sum: 1 + 23 + 4 + 5 => 33
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 33 - 15 | => 18

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

Element sum: 1 + 2 + 3 + 4 + 5 => 15
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 15 - 15 | => 0

Example 3
Input: @ints = (1, 2, 34)
Output: 27

Element sum: 1 + 2 + 34 => 37
Digit sum: 1 + 2 + 3 + 4 => 10
Absolute difference: | 37 - 10 | => 27

The element sum is larger or equal than the digit sum, so I just do both sums and subtract them. This takes a half-liner.

Example 1

perl -MList::Util=sum0 -E '
$d=sum0 map {split ""} @ARGV; say "@ARGV -> ", sum0(@ARGV)-$d;
' 1 23 4 5

Results:

1 23 4 5 -> 18

Example 2:

perl -MList::Util=sum0 -E '
$d=sum0 map {split ""} @ARGV; say "@ARGV -> ", sum0(@ARGV)-$d;
' 1 2 3 4 5

Results:

1 2 3 4 5 -> 0

Example 3:

perl -MList::Util=sum0 -E '
$d=sum0 map {split ""} @ARGV; say "@ARGV -> ", sum0(@ARGV)-$d;
' 1 2 34

Results:

1 2 34 -> 27

The full code is similar, with a couple of tests:

 1  # Perl weekly challenge 320
 2  # Task 2:  Sum Difference
 3  #
 4  # See https://wlmb.github.io/2025/05/05/PWC320/#task-2-sum-difference
 5  use v5.36;
 6  use List::Util qw(sum0 all);
 7  die <<~"FIN" unless @ARGV;
 8      Usage: $0 N1 N2...
 9      to compute the absolute value between the sum of the positive
10      intengers N1 N2... and the sum of their digits.
11      FIN
12  my @digits = map {split ""} @ARGV;
13  die "Only digits allowed" unless all {/[0-9]/} @digits;
14  say "@ARGV -> ", sum0(@ARGV)-sum0(@digits);

Examples:

./ch-2.pl 1 23 4 5
./ch-2.pl 1 2 3 4 5
./ch-2.pl 1 2 34

Results:

1 23 4 5 -> 18
1 2 3 4 5 -> 0
1 2 34 -> 27

/;

Written on May 5, 2025