Perl Weekly Challenge 261: Element Digit Sum

These are some answers to the Week 261, Task 1, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on March 24, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Element Digit Sum

You are given an array of integers, @ints.

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

Example 1

Input: @ints = (1,2,3,45)
Output: 36

Element Sum: 1 + 2 + 3 + 45 = 51
Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
Absolute Difference: | 51 - 15 | = 36

Example 2

Input: @ints = (1,12,3)
Output: 9

Element Sum: 1 + 12 + 3 = 16
Digit Sum: 1 + 1 + 2 + 3 = 7
Absolute Difference: | 16 - 7 | = 9

Example 3

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

Element Sum: 1 + 2 + 3 + 4 = 10
Digit Sum: 1 + 2 + 3 + 4 = 10
Absolute Difference: | 10 - 10 | = 0

Example 4

Input: @ints = (236, 416, 336, 350)
Output: 1296

I'm not sure we need to use the absolute value of the difference, as I think the element sum will never be less than the digit sum, but it doesn't hurt using it.

Element Digit Sum in Raku

Using the Raku built-in sum, comb, and flat, methods leads to a simple one-liner solution.

sub element-digit-sum (@in) {
    return (@in.sum - @in.map({.comb}).flat.sum).abs;
}

my @tests = <1 2 3 45>, <1 12 3>, <1 2 3 4>, <236 416 336 350>;
for @tests -> @test {
    printf "%-20s => ", "@test[]";
    say element-digit-sum @test;
}

This program displays the following output:

$ raku ./element-digit-sum.raku
1 2 3 45             => 36
1 12 3               => 9
1 2 3 4              => 0
236 416 336 350      => 1296

Element Digit Sum in Perl

This is a port to Perl of the above Raku program. The only significant change is that we had to implement our own sum subroutine.

use strict;
use warnings;
use feature 'say';

sub sum {
    my $sum = 0;
    $sum += $_ for @_;
    return $sum;
}

sub element_digit_sum {
    my @in = @_;
    return abs(sum (@in) - sum (map {split //, $_} @in));
}

my @tests = ( [<1 2 3 45>], [<1 12 3>], [<1 2 3 4>],
              [<236 416 336 350>] );
for my $test (@tests) {
    printf "%-20s => ", "@$test";
    say element_digit_sum @$test;
}

This program displays the following output:

$ perl ./element-digit-sum.pl
1 2 3 45             => 36
1 12 3               => 9
1 2 3 4              => 0
236 416 336 350      => 1296

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on March 31, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.