Perl Weekly Challenge 225.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 225.
Task 1: Max Words
Submitted by: Mohammad S Anwar
You are given a list of sentences, @list.
A sentence is a list of words that are separated by a single space with no leading
or trailing spaces.
Write a script to find out the maximum number of words that appear in a single
sentence.
Example 1
Input: @list = (qw/Perl and Raku belong to the same family./,
qw/I love Perl./,
qw/The Perl and Raku Conference./)
Output: 8
Example 2
Input: @list = (qw/The Weekly Challenge./,
qw/Python is the most popular guest language./,
qw/Team PWC has over 300 members./)
Output: 7
I simply split into words, count them and find the max. This fits a simple oneliner.
Example 1:
perl -MList::Util=max -E 'say join "\n", @ARGV, " -> ", max map {0+@{[split " "]}} @ARGV' \
"Perl and Raku belong to the same family." \
"I love Perl." \
"The Perl and Raku Conference."
Results:
Perl and Raku belong to the same family.
I love Perl.
The Perl and Raku Conference.
->
8
Example 2:
perl -MList::Util=max -E 'say join "\n", @ARGV, " -> ", max map {0+@{[split " "]}} @ARGV' \
"The Weekly Challenge." \
"Python is the most popular guest language." \
"Team PWC has over 300 members."
Results:
The Weekly Challenge.
Python is the most popular guest language.
Team PWC has over 300 members.
->
7
The full code is:
1 # Perl weekly challenge 225
2 # Task 1: Max Words
3 #
4 # See https://wlmb.github.io/2023/07/09/PWC225/#task-1-max-words
5 use v5.36;
6 use List::Util qw(max);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 S1 [S2...]
9 to count the maximum number of space separated words in sentences S1 S2...
10 FIN
11 say join "\n", @ARGV, " -> ", max map {0+@{[split " "]}} @ARGV;
Example 1:
./ch-1.pl "Perl and Raku belong to the same family." \
"I love Perl." \
"The Perl and Raku Conference."
Results:
Perl and Raku belong to the same family.
I love Perl.
The Perl and Raku Conference.
->
8
Example 2:
./ch-1.pl "The Weekly Challenge." \
"Python is the most popular guest language." \
"Team PWC has over 300 members."
Results:
The Weekly Challenge.
Python is the most popular guest language.
Team PWC has over 300 members.
->
7
Task 2: Left Right Sum Diff
Submitted by: Mohammad S Anwar
You are given an array of integers, @ints.
Write a script to return left right sum diff array as shown below:
@ints = (a, b, c, d, e)
@left = (0, a, (a+b), (a+b+c))
@right = ((c+d+e), (d+e), e, 0)
@left_right_sum_diff = ( | 0 - (c+d+e) |,
| a - (d+e) |,
| (a+b) - e |,
| (a+b+c) - 0 | )
Example 1:
Input: @ints = (10, 4, 8, 3)
Output: (15, 1, 11, 22)
@left = (0, 10, 14, 22)
@right = (15, 11, 3, 0)
@left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|)
= (15, 1, 11, 22)
Example 2:
Input: @ints = (1)
Output: (0)
@left = (0)
@right = (0)
@left_right_sum_diff = ( |0-0| ) = (0)
Example 3:
Input: @ints = (1, 2, 3, 4, 5)
Output: (14, 11, 6, 1, 19)
@left = (0, 1, 3, 6, 10)
@right = (14, 12, 9, 5, 0)
@left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|)
= (14, 11, 6, 1, 10)
It is not clear to me how many members should the @right
and @left
arrays contain. In the description before the examples there are 5
@ints
and 4 @left
and @right
members, but in the examples there
is the same number of members in each array. I’ll assume that is the
rule. I start by appending zeroes to both sides of the input
array. Then I use the reductions
from List::Util
to make the sums
leading to the left and right arrays. Finally, I subtract
corresponding terms and take the absolute value to obtain the
result. The code fits a two-liner.
Example 1:
perl -MList::Util=reductions -E '
@x=(0, @ARGV, 0); $n=@x-3; ($l,$r)=map {[reductions {$a+$b} @$_]} [@x[0..$n]],
[((reverse @x)[0..$n])]; @o=map {abs($l->[$_]-$r->[-$_-1])} 0..$n; say "@ARGV -> @o";
' 10 4 8 3
Results:
10 4 8 3 -> 15 1 11 22
Example 2:
perl -MList::Util=reductions -E '
@x=(0, @ARGV, 0); $n=@x-3; ($l,$r)=map {[reductions {$a+$b} @$_]} [@x[0..$n]],
[((reverse @x)[0..$n])]; @o=map {abs($l->[$_]-$r->[-$_-1])} 0..$n; say "@ARGV -> @o";
' 1
Results:
1 -> 0
Example 3:
perl -MList::Util=reductions -E '
@x=(0, @ARGV, 0); $n=@x-3; ($l,$r)=map {[reductions {$a+$b} @$_]} [@x[0..$n]],
[((reverse @x)[0..$n])]; @o=map {abs($l->[$_]-$r->[-$_-1])} 0..$n; say "@ARGV -> @o";
' 1 2 3 4 5
Results:
1 2 3 4 5 -> 14 11 6 1 10
(Note there is a mistake in the problem statement regarding this example.)
The full code is similar, but maybe more understandable.
1 # Perl weekly challenge 225
2 # Task 2: Left Right Sum Diff
3 #
4 # See https://wlmb.github.io/2023/07/09/PWC225/#task-2-left-right-sum-diff
5 use v5.36;
6 use List::Util qw(reductions);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 N1 [N2...]
9 to find the differences of the right and left sums of the list of
10 numbers N1 N2...
11 FIN
12 my @input= (0, @ARGV, 0);
13 my $last_index=@input-3;
14 my ($left, $right) = map {[reductions {$a+$b} @$_]}
15 [@input[0..$last_index]], [((reverse @input)[0..$last_index])];
16 my @output = map {abs($left->[$_] - $right->[-$_-1])} 0..$last_index;
17 say "@ARGV -> @output";
Example 1:
./ch-2.pl 10 4 8 3
Results:
10 4 8 3 -> 15 1 11 22
Example 2:
./ch-2.pl 1
Results:
1 -> 0
Example 3:
./ch-2.pl 1 2 3 4 5
Results:
1 2 3 4 5 -> 14 11 6 1 10