Perl Weekly Challenge 251: Concatenation Value

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

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on January 14, 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: Concatenation Value

You are given an array of integers, @ints.

Write a script to find the concatenation value of the given array.

The concatenation of two numbers is the number formed by concatenating their numerals.

For example, the concatenation of 10, 21 is 1021.
The concatenation value of @ints is initially equal to 0.
Perform this operation until @ints becomes empty:

If there exists more than one number in @ints, pick the first element
and last element in @ints respectively and add the value of their
concatenation to the concatenation value of @ints, then delete the
first and last element from @ints.

If one element exists, add its value to the concatenation value of
@ints, then delete it.

Example 1

Input: @ints = (6, 12, 25, 1)
Output: 1286

1st operation: concatenation of 6 and 1 is 61
2nd operation: concaternation of 12 and 25 is 1225

Concatenation Value => 61 + 1225 => 1286

Example 2

Input: @ints = (10, 7, 31, 5, 2, 2)
Output: 489

1st operation: concatenation of 10 and 2 is 102
2nd operation: concatenation of 7 and 2 is 72
3rd operation: concatenation of 31 and 5 is 315

Concatenation Value => 102 + 72 + 315 => 489

Example 3

Input: @ints = (1, 2, 10)
Output: 112

1st operation: concatenation of 1 and 10 is 110
2nd operation: only element left is 2

Concatenation Value => 110 + 2 => 112

Concatenation Value in Raku

So long as there are 2 or more items in the input array, we remove from the array and retrieve the first and last item (with the shift and pop methods), concatenate their values, and add the result to the $concat accumulator. At the end, we add the last item (if any) to the accumulator.

sub concat-vals (@in is copy) {
    my $concat;
    while @in.elems > 1 {
        $concat += @in.shift ~ @in.pop;
    }
    $concat += shift @in if @in.elems > 0; # last item if any
    return $concat;
}        

for <6 12 25 1>, <10 7 31 5 2 2>, <1 2 10> -> @test {
    printf "%-15s => ", "@test[]";
    say concat-vals @test;
}

This program displays the following output:

$ raku ./concat-values.raku
6 12 25 1       => 1286
10 7 31 5 2 2   => 489
1 2 10          => 112

Concatenation Value in Raku

This is a port to Perl of the above Raku program. Please refer to the previous section if you need any explanations.

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

sub concat_vals {
    my @in = @_;
    my $concat;
    while (@in > 1) {
        $concat += (shift @in) . (pop @in);
    }
    $concat += shift @in if @in > 0; # if we have 1 item left
    return $concat;
}        

for my $test ([<6 12 25 1>], [<10 7 31 5 2 2>], [<1 2 10>]) {
    printf "%-15s => ", "@$test";
    say concat_vals @$test;
}

This program displays the following output:

$ perl ./concat-values.pl
6 12 25 1       => 1286
10 7 31 5 2 2   => 489
1 2 10          => 112

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 January 21, 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.