Count Difference
with Raku

by Arne Sommer

Count Difference with Raku

[342] Published 11. May 2025.

This is my response to The Weekly Challenge #320.

Challenge #320.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.

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.
File: maximum-count
#! /usr/bin/env raku

unit sub MAIN (*@ints where @ints.elems > 1 && all(@ints) ~~ Int,  # [1]
               :v(:$verbose));

my $pos = @ints.grep( * > 0 ).elems;                               # [2]
my $neg = @ints.grep( * < 0 ).elems;                               # [3]

say ": There are $pos positive integers\n: There are $neg negative integers"
  if $verbose;

say ($pos, $neg).max;                                              # [4]

[1] A slurpy array with at least two values, and they must be of the Int type.

[2] Use grep to get the positive values, and then elems to count them.

See docs.raku.org/routine/grep for more information about grep.

See docs.raku.org/routine/elems for more information about elems.

[3] Ditto for the negative values.

[4] Print the highest count, using max.

See docs.raku.org/routine/max for more information about max.

Running it, and note the -- argument stopper used on example 1 and 2 to prevent the parsing of negative values as command line arguments.

$ ./maximum-count -- -3 -2 -1 1 2 3
3

$ ./maximum-count -- -2 -1 0 0 1
2

$ ./maximum-count -v 1 2 3 4
4

Looking good.

With verbose mode:

$ ./maximum-count -v -- -3 -2 -1 1 2 3
: There are 3 positive integers
: There are 3 negative integers
3

$ ./maximum-count -v -- -2 -1 0 0 1
: There are 1 positive integers
: There are 2 negative integers
2

$ ./maximum-count -v 1 2 3 4
: There are 4 positive integers
: There are 0 negative integers
4

Challenge #320.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.

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
File: sum-difference
#! /usr/bin/env raku

unit sub MAIN (*@ints where @ints.elems > 0
                 && all(@ints) ~~ Int
                 && all(@ints) > 0,         # [1]
               :v(:$verbose));

my $elem-sum = @ints.sum;                   # [2]
my $digit-sum = @ints>>.comb>>.sum.sum;     # [3]

if $verbose
{
  say ": Element sum: $elem-sum";
  say ": Digit sum: $digit-sum";
}

say abs($elem-sum - $digit-sum);            # [4]

[1] A slurpy array with at least one value, and they must be of the Int type and be positive.

[2] Add all the values together with sum.

[3] Apply comb on all the values (the >>. construct), then sum on those digits (the second >>. construct), and finally sum again on all the sums.

See docs.raku.org/routine/comb for more information about comb.

[4] Print the difference, without a sign (if any) with abs.

See docs.raku.org/routine/abs for more information about abs.

Running it:

$ ./sum-difference 1 23 4 5
18

$ ./sum-difference 1 2 3 4 5
0

$ ./sum-difference 1 2 34
27

Looking good.

With verbose mode:

$ ./sum-difference -v 1 23 4 5
: Element sum: 33
: Digit sum: 15
18

$ ./sum-difference -v 1 2 3 4 5
: Element sum: 15
: Digit sum: 15
0

$ ./sum-difference -v 1 2 34
: Element sum: 37
: Digit sum: 10
27

And that's it.