The weekly challenge 251 - Task 1: Concatenation Value

 1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1
 3 #
 4 # Task 1: Concatenation Value
 5 # ===========================
 6 #
 7 # You are given an array of integers, @ints.
 8 #
 9 # Write a script to find the concatenation value of the given array.
10 #
11 # The concatenation of two numbers is the number formed by concatenating their
12 # numerals.
13 #
14 ## For example, the concatenation of 10, 21 is 1021.
15 ## The concatenation value of @ints is initially equal to 0.
16 ## Perform this operation until @ints becomes empty:
17 ##
18 ## If there exists more than one number in @ints, pick the first element
19 ## and last element in @ints respectively and add the value of their
20 ## concatenation to the concatenation value of @ints, then delete the
21 ## first and last element from @ints.
22 ##
23 ## If one element exists, add its value to the concatenation value of
24 ## @ints, then delete it.
25 #
26 ## Example 1
27 ##
28 ## Input: @ints = (6, 12, 25, 1)
29 ## Output: 1286
30 ##
31 ## 1st operation: concatenation of 6 and 1 is 61
32 ## 2nd operation: concaternation of 12 and 25 is 1225
33 ##
34 ## Concatenation Value => 61 + 1225 => 1286
35 #
36 ## Example 2
37 ##
38 ## Input: @ints = (10, 7, 31, 5, 2, 2)
39 ## Output: 489
40 ##
41 ## 1st operation: concatenation of 10 and 2 is 102
42 ## 2nd operation: concatenation of 7 and 2 is 72
43 ## 3rd operation: concatenation of 31 and 5 is 315
44 ##
45 ## Concatenation Value => 102 + 72 + 315 => 489
46 #
47 ## Example 3
48 ##
49 ## Input: @ints = (1, 2, 10)
50 ## Output: 112
51 ##
52 ## 1st operation: concatenation of 1 and 10 is 110
53 ## 2nd operation: only element left is 2
54 ##
55 ## Concatenation Value => 110 + 2 => 112
56 #
57 ############################################################
58 ##
59 ## discussion
60 ##
61 ############################################################
62 #
63 # Starting with a sum of 0, we call a function that just concatenates
64 # the first and last elements of @ints and adds that number to the
65 # current sum, then we call the function recursively with the new
66 # current sum and the remainder of the array. Of course handle the
67 # case where there is only one element in the array by returning the
68 # current sum + the last element, and the case of an empty array by
69 # returning the current sum.
70 
71 use strict;
72 use warnings;
73 
74 concatenation_value(6, 12, 25, 1);
75 concatenation_value(10, 7, 31, 5, 2, 2);
76 concatenation_value(1, 2, 10);
77 
78 sub concatenation_value {
79    my @ints = @_;
80    print "Input: (" . join(", ", @ints) . ")\n";
81    my $result = concatenation_value_(0, @ints);
82    print "Output: $result\n";
83 }
84 
85 sub concatenation_value_ {
86    my ($current, @ints) = @_;
87    return $current unless @ints;
88    if(@ints >= 2) {
89       my $first = shift @ints;
90       my $last = pop @ints;
91       return concatenation_value_($current + "$first$last", @ints);
92    } else {
93       return $current + $ints[0];
94    }
95 }