Perl Weekly Challenge 261.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 261.
Task 1: Element Digit Sum
Submitted by: Mohammad Sajid Anwar
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 just follow the instructions, using split
to get the individual
digits and sum0
from List::Util
to sum all the numbers. This
yields a half liner:
perl -MList::Util=sum0 -E 'say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV;' 1 2 3 45
Results:
1 2 3 45 -> 36
perl -MList::Util=sum0 -E 'say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV;' 1 12 3
Results:
1 12 3 -> 9
perl -MList::Util=sum0 -E 'say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV;' 1 2 3 4
Results:
1 2 3 4 -> 0
perl -MList::Util=sum0 -E 'say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV;' 236 416 336 350
Results:
236 416 336 350 -> 1296
The corresponding full code follows:
1 # Perl weekly challenge 261
2 # Task 1: Element Digit Sum
3 #
4 # See https://wlmb.github.io/2024/03/18/PWC261/#task-1-element-digit-sum
5 use v5.36;
6 use List::Util qw(sum0);
7 die <<~"FIN" unless @ARGV;
8 usage: $0 N1 [N2...]
9 to find the diference between the sum of the numbers N1+N2+...
10 and the sum of their digits
11 FIN
12 say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV;
Example:
./ch-1.pl 1 2 3 45
./ch-1.pl 1 12 3
./ch-1.pl 1 2 3 4
./ch-1.pl 236 416 336 350
Results:
1 2 3 45 -> 36
1 12 3 -> 9
1 2 3 4 -> 0
236 416 336 350 -> 1296
Task 2: Multiply by Two
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints and an integer $start..
Write a script to do the followings:
a. Look for $start in the array @ints, if found multiply the number by 2
b. If not found stop the process otherwise repeat
In the end return the final value.
Example 1
Input: @ints = (5,3,6,1,12) and $start = 3
Output: 24
Step 1: 3 is in the array so 3 x 2 = 6
Step 2: 6 is in the array so 6 x 2 = 12
Step 3: 12 is in the array so 12 x 2 = 24
24 is not found in the array so return 24.
Example 2
Input: @ints = (1,2,4,3) and $start = 1
Output: 8
Step 1: 1 is in the array so 1 x 2 = 2
Step 2: 2 is in the array so 2 x 2 = 4
Step 3: 4 is in the array so 4 x 2 = 8
8 is not found in the array so return 8.
Example 3
Input: @ints = (5,6,7) and $start = 2
Output: 2
2 is not found in the array so return 2.
I use first
from List::Util
to find if the desired number is in
the list. This yields a one-liner.
perl -MList::Util=first -E '
($s, @i)=@ARGV; print "Start=$s, ints=@i -> "; $s*=2 while defined first {$_==$s} @i; say $s' 3 5 3 6 1 12
Results:
Start=3, ints=5 3 6 1 12 -> 24
perl -MList::Util=first -E '
($s, @i)=@ARGV; print "Start=$s, ints=@i -> "; $s*=2 while defined first {$_==$s} @i; say $s' 1 1 2 4 3
Results:
Start=1, ints=1 2 4 3 -> 8
perl -MList::Util=first -E '
($s, @i)=@ARGV; print "Start=$s, ints=@i -> "; $s*=2 while defined first {$_==$s} @i; say $s' 2 5 6 7
Results:
Start=2, ints=5 6 7 -> 2
The full code is:
1 # Perl weekly challenge 261
2 # Task 2: Multiply by Two
3 #
4 # See https://wlmb.github.io/2024/03/18/PWC261/#task-2-multiply-by-two
5 use v5.36;
6 use List::Util qw(first);
7 die <<~"FIN" unless @ARGV >= 2;
8 Usage: $0 S N1 [N2...]
9 to find the smallest number S*2^n that is not in the list N1 N2...
10 FIN
11 my ($start, @ints)=@ARGV;
12 print "Start=$start, ints=@ints -> ";
13 $start *= 2 while defined first {$_==$start} @ints;
14 say $start;
Example:
./ch-2.pl 3 5 3 6 1 12
./ch-2.pl 1 1 2 4 3
./ch-2.pl 2 5 6 7
Results:
Start=3, ints=5 3 6 1 12 -> 24
Start=1, ints=1 2 4 3 -> 8
Start=2, ints=5 6 7 -> 2
/;
Written on March 18, 2024