Robbie Hatley's Solutions To The Weekly Challenge #225

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2023-07-09 through 2023-07-15) is weekly challenge #225.

Task 1 is as follows:

Task 1: Max Words
Submitted by: Mohammad S Anwar
Given a list of sentences, write a script to find 
the maximum word count of the sentences of the list.

Example 1:
Input:
   @list =
   (
      "Perl and Raku belong to the same family.",
      "I love Perl.",
      "The Perl and Raku Conference."
   );
Output: 8

Example 2:
Input:
   @list =
   (
      "The Weekly Challenge.",
      "Python is the most popular guest language.",
      "Team PWC has over 300 members.",
   );
Output: 7

This is just a matter of using split to parse the sentences of each list to words, then counting the words, then using List::Util 'max' to find the maximum word count of any sentence in the list. Something like this:

   my $max = max map {scalar split /[ .]+/, $_} @sentences;

The script I came up with is this:

Robbie Hatley's Solution to The Weekly Challenge 225-1

Task 2 is as follows:

Task 2: Left Right Sum Diff
Submitted by: Mohammad S Anwar
You are given an array of integers, @ints.
Write a script to return the left right sum diff array as shown below:
@ints  = (a, b, c, d, e)
@left  = (     0,         a,   (a+b), (a+b+c), (a+b+c+d) )
@right = ( (b+c+d+e), (c+d+e), (d+e),    e,        0     )
@left_right_sum_diff =
(
   |   0       - (b+c+d+e) |,
   |   a       -  (c+d+e)  |,
   | (a+b)     -   (d+e)   |,
   | (a+b+c)   -     e     |,
   | (a+b+c+d) -     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)

This is just a matter of making subs to compute "left" and "right" sequences of partial sums of as defined by this problem (which is NOT the way math textbooks define "sequence of partial sums", by the way), then computing the array of absolute values of index-wise differences between @left and @right.

The script I came up with is this:

Robbie Hatley's Solution to The Weekly Challenge 225-2

That's it for 225; see you on 226!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273