Peter
Peter Campbell Smith

Lucky concatenations

Weekly challenge 251 — 8 January 2024

Week 251 - 8 Jan 2024

Task 1

Task — 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.

Examples


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

Analysis

I took a simple approach: loop over @ints inwards from both ends, successively adding the concatenations to $sum until either (a) all values have been used, or (b) only one value remains, in which case add it to $sum.

I suppose this could be claimed to be non-compliant as I haven't deleted the members from the array as I used them, but I reckon that if I produce the correct output then the task is accomplished.

Try it 

Try running the script with any input:



example: 1, 2, 3, 4, 5, 6, 7

Script


#!/usr/bin/perl

use v5.16;    # The Weekly Challenge - 2024-01-08
use utf8;     # Week 251 task 1 - Concatenation value
use strict;   # Peter Campbell Smith
use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

binmode STDOUT, ':utf8';
my ($j, @ints);

concatenation_value(6, 12, 25, 1);
concatenation_value(10, 7, 31, 5, 2, 2);
concatenation_value(1, 2, 10);

for $j (0 .. 99) {
    push(@ints, int(rand(99)));
}
concatenation_value(@ints);

sub concatenation_value {
    
    my(@ints, $sum, $j, $k);
    
    # initialise
    @ints = @_;
    $sum = 0;
    
    # iterate from both ends
    for $j (0 .. @ints) {
        $k = @ints - $j - 1;
        last if $k < $j;
        $sum += ($k == $j ? $ints[$j] : $ints[$j] . $ints[$k]);
    }
    
    # show result
    say qq[\nInput:  (] . join(', ', @ints) . ')';
    say qq[Output: $sum];
}

Output


Input:  (6, 12, 25, 1)
Output: 1286

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

Input:  (1, 2, 10)
Output: 112

Input:  (56, 1, 44, 58, 9, 95, 74, 29, 31, 67, 91, 44, 88, 71, 83, 49, 67, 95, 22, 39, 32, 74, 57, 6, 14, 35, 12, 24, 11, 23, 14, 80, 6, 51, 56, 10, 30, 43, 0, 51, 93, 72, 76, 50, 87, 39, 42, 97, 2, 68, 26, 85, 67, 82, 46, 28, 34, 64, 74, 40, 13, 72, 72, 91, 31, 54, 1, 4, 78, 69, 21, 88, 42, 91, 88, 16, 28, 13, 72, 60, 32, 56, 68, 8, 82, 95, 25, 5, 1, 52, 73, 95, 27, 51, 7, 7, 25, 91, 38, 19)
Output: 206807

The content of this website which has been created by
Peter Campbell Smith is hereby placed in the public domain