Perl Weekly Challenge 258.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 258.
Task 1: Count Even Digits Number
Submitted by: Mohammad Sajid Anwar
You are given a array of positive integers, @ints.
Write a script to find out how many integers have even number of digits.
Example 1
Input: @ints = (10, 1, 111, 24, 1000)
Output: 3
There are 3 integers having even digits i.e. 10, 24 and 1000.
Example 2
Input: @ints = (111, 1, 11111)
Output: 0
Example 3
Input: @ints = (2, 8, 1024, 256)
Output: 1
I just have to count the digits of each input modulo 2 and use the result to filter numbers with even number of digits and count them. This fits a short half-liner.
Example 1:
perl -E 'say "@ARGV -> ", 0+grep {(split "")%2==0}@ARGV' 10 1 111 24 1000
Results:
10 1 111 24 1000 -> 3
Example 2:
perl -E 'say "@ARGV -> ", 0+grep {(split "")%2==0}@ARGV' 111 1 11111
Results:
111 1 11111 -> 0
Example 3:
perl -E 'say "@ARGV -> ", 0+grep {(split "")%2==0}@ARGV' 2 8 1024 246
Results:
2 8 1024 246 -> 1
The corresponding full code is
1 # Perl weekly challenge 258
2 # Task 1: Count Even Digits Number
3 #
4 # See https://wlmb.github.io/2024/02/26/PWC258/#task-1-count-even-digits-number
5 use v5.36;
6 use List::Util qw(all);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 N1 [N2...]
9 to count how many input numbers have an even number of digits.
10 FIN
11 die "Only digits allowed" unless all {/^\d+$/} @ARGV;
12 say "@ARGV -> ", 0+grep {(split "")%2==0}@ARGV;
Examples:
./ch-1.pl 10 1 111 24 1000
./ch-1.pl 111 1 11111
./ch-1.pl 2 8 1024 246
Results:
10 1 111 24 1000 -> 3
111 1 11111 -> 0
2 8 1024 246 -> 1
Task 2: Sum of Values
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @int and an integer $k.
Write a script to find the sum of values whose index binary representation
has exactly $k number of 1-bit set.
Example 1
Input: @ints = (2, 5, 9, 11, 3), $k = 1
Output: 17
Binary representation of index 0 = 0
Binary representation of index 1 = 1
Binary representation of index 2 = 10
Binary representation of index 3 = 11
Binary representation of index 4 = 100
So the indices 1, 2 and 4 have total one 1-bit sets.
Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17
Example 2
Input: @ints = (2, 5, 9, 11, 3), $k = 2
Output: 11
Example 3
Input: @ints = (2, 5, 9, 11, 3), $k = 0
Output: 2
(There is a mistake in the explanation of example 1: $ints[3]
should
have been $ints[4]
)
I can filter all the indices according to their number of bits, and
then sum the corresponding values. This fits a one-liner.
Example 1:
perl -MList::Util=sum0 -E '
($k,@v)=@ARGV; say "k=$k, ints=@v -> ", sum0 @v[grep {$k==sum0 split "", sprintf "%b",$_}0..@v-1];
' 1 2 5 9 11 3
Results:
k=1, ints=2 5 9 11 3 -> 17
Example 2:
perl -MList::Util=sum0 -E '
($k,@v)=@ARGV; say "k=$k, ints=@v -> ", sum0 @v[grep {$k==sum0 split "", sprintf "%b",$_}0..@v-1];
' 2 2 5 9 11 3
Results:
k=2, ints=2 5 9 11 3 -> 11
Example 3:
perl -MList::Util=sum0 -E '
($k,@v)=@ARGV; say "k=$k, ints=@v -> ", sum0 @v[grep {$k==sum0 split "", sprintf "%b",$_}0..@v-1];
' 0 2 5 9 11 3
Results:
k=0, ints=2 5 9 11 3 -> 2
The full code follows:
1 # Perl weekly challenge 258
2 # Task 2: Sum of Values
3 #
4 # See https://wlmb.github.io/2024/02/26/PWC258/#task-2-sum-of-values
5 use v5.36;
6 use List::Util qw(sum0);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 K N0 [N1...]
9 to sum the values of Nm whose index m has K ones in its binary representation
10 FIN
11 my ($k,@int)=@ARGV;
12 # Convert to binary, add ones and compare to $k to filter indices
13 my @indices=grep {$k==sum0 split "", sprintf "%b",$_}0..@int-1;
14 my $result=sum0 @int[@indices];
15 say "k=$k, ints=@int -> ", $result;
Example:
./ch-2.pl 1 2 5 9 11 3
./ch-2.pl 2 2 5 9 11 3
./ch-2.pl 0 2 5 9 11 3
Results:
k=1, ints=2 5 9 11 3 -> 17
k=2, ints=2 5 9 11 3 -> 11
k=0, ints=2 5 9 11 3 -> 2
/;
Written on February 26, 2024