RabbitFarm
20240323
These Elements, They’re Multiplying!
The examples used here are from the weekly challenge problem statement and demonstrate the working solution.
File Index
Part 1: Element Digit Sum
You are given an array of integers, @integers. Write a script to evaluate the absolute difference between every element and the digit sum of the entire given array.
The complete solution is contained in one file that has a simple structure.
For this problem we do not need to include very much. We’re just specifying to use the current version of Perl, for all the latest features in the language. This fragment is also used in Part 2.
First, let’s consider how we compute the digit sum for an array of integers. If we we make sure that all multidigit numbers are expanded into lists of digits then this is the sum of the concatenation of all such lists, along with single digit numbers.
The expansion of multidigit numbers is handled by map, and the sum is taken with unpack and the resulting final array. A key thing to remember here is that Perl will flatten all lists inside the array so all the results from the map will be in a list of single digits.
The element sum is the same procedure as the digit sum, but just without the map.
Finally, we have a few lines of code for running some tests.

MAIN:{
say element_digit_sum 1, 2, 3, 45;
say element_digit_sum 1, 12, 3;
say element_digit_sum 1, 2, 3, 4;
say element_digit_sum 236, 416, 336, 350;
}
◇

Fragment referenced in 1.
Sample Run
$ perl perl/ch1.pl 36 9 0 1296
Part 2: Multiply by Two
You are given an array of integers, @integers and an integer $start. Write a script to do the following:
a) Look for $start in the array @integers, if found multiply the number by 2.
b) If not found stop the process, otherwise repeat.
In the end return the final value.
We’ll contain the solution in a single recursive function. The completed solution will just have that function plus a few tests.
This is our principal function. As can be seen, it’s very short! The logic here is simple: for each recursive call check for $start in the array and, if found, double $start and keep recursing. Otherwise, return $start.

sub search_multiply{
my($start) = shift;
return $start if 0 == grep {$start == $_}
@_;
search_multiply($start + $start,
@_);
}
◇

Fragment referenced in 7.
Finally, here’s a few tests to confirm everything is working right.

MAIN:{
say search_multiply 3, 5, 3, 6, 1, 12;
say search_multiply 1, 1, 2, 3, 4;
say search_multiply 2, 5, 6, 7;
}
◇

Fragment referenced in 7.
Sample Run
$ perl ch2.pl 24 8 2
References
posted at: 20:34 by: Adam Russell  path: /perl  permanent link to this entry