RabbitFarm

2025-05-10

Summit of Count Deviation

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

Part 1: Maximum Count

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.

Our solution will be pretty short, contained in just a single file that has the following structure.

"ch-1.pl" 1


preamble 2
filter and count the positive/negative numbers, compute maximum 4
main 3

The preamble is just whatever we need to include. Here we aren’t using anything special, just specifying the latest Perl version.

preamble 2 ⟩≡


use v5.40;

Fragment referenced in 1, 7.

the main section is just some basic tests.

main 3 ⟩≡


MAIN:{
say maximum_count -3, -2, -1, 1, 2, 3;
say maximum_count -2, -1, 0, 0, 1;
say maximum_count 1, 2, 3, 4;
}

Fragment referenced in 1.

All the work is done in the following subroutine.

filter and count the positive/negative numbers, compute maximum 4 ⟩≡


sub maximum_count{
my @numbers = @_;
filter negatives 5
filter positives 6
return (sort {$b <=> $a} ($positives, $negatives))[0];
}

Fragment referenced in 1.

Defines: @numbers 5, 6.

Uses: $negatives 5, $positives 6.

We do the filtering with a grep.

filter negatives 5 ⟩≡


my $negatives = 0 + grep {$_ < 0} @numbers;

Fragment referenced in 4.

Defines: $negatives 4.

Uses: @numbers 4.

filter positives 6 ⟩≡


my $positives = 0 + grep {$_ > 0} @numbers;

Fragment referenced in 4.

Defines: $positives 4.

Uses: @numbers 4.

Sample Run
$ perl perl/ch-1.pl 
3 
2 
4
    

Part 2: Sum Difference

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.

Our solution will be pretty short, contained in just a single file that has the following structure.

"ch-2.pl" 7


preamble 2
compute the digit sum and element sum and then subtract 9
main 8

The main section is just some basic tests.

main 8 ⟩≡


MAIN:{
say sum_difference 1, 23, 4, 5;
say sum_difference 1, 2, 3, 4, 5;
say sum_difference 1, 2, 34;
}

Fragment referenced in 7.

All the work is done in the following subroutine.

compute the digit sum and element sum and then subtract 9 ⟩≡


sub sum_difference{
my @numbers = @_;
digit sum 10
element sum 11
return abs($digit_sum - $element_sum);
}

Fragment referenced in 7.

Defines: @numbers 10, 11.

Uses: $digit_sum 10, $element_sum 11.

We compute the digit sum by splitting each element as a string and then summing the list of digits.

digit sum 10 ⟩≡


my @digits;
do{
push @digits, split //, $_;
} for @numbers;
my $digit_sum = unpack(q/%32I*/, pack(q/I*/, @digits));

Fragment referenced in 9.

Defines: $digit_sum 9.

Uses: @numbers 9.

The element sum is a straightforward summing of the elements.

element sum 11 ⟩≡


my $element_sum = unpack(q/%32I*/, pack(q/I*/, @numbers));

Fragment referenced in 9.

Defines: $element_sum 9.

Uses: @numbers 9.

Sample Run
$ perl perl/ch-2.pl 
18 
0 
27
    

References

The Weekly Challenge 320
Generated Code

posted at: 19:15 by: Adam Russell | path: /perl | permanent link to this entry