Weekly Challenge 225
Tags: perl, python, theweeklychallenge
Published: 2023-07-16 13:09:39


Each week Mohammad  S. Anwar sends out [The Weekly Challenge](https://theweeklychallenge.org/), a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

[Challenge](https://theweeklychallenge.org/blog/perl-weekly-challenge-225/), [My solutions](https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-225/sgreen)

## Task 1: Max Words

### Task

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.

### My solution

This is pretty straight forward. We know the number of words is one more than the number of spaces in each sentence. Therefore I simply count the number of spaces in the `sentences` list in a variable called `words`. I then print the maximum value of that list.

In Python, we can use the count method to count the number of space. Meanwhile a Perl hack is to the use [tr](https://perldoc.perl.org/perlop#tr/SEARCHLIST/REPLACEMENTLIST/cdsr) method to replace spaces with spaces. `tr` returns the number of occurrences found.

### Examples

```bash
$ ./ch-1.py "Perl and Raku belong to the same family.", "I love Perl.", "The Perl and Raku Conference."
8

$ ./ch-1.py "The Weekly Challenge.", "Python is the most popular guest language.", "Team PWC has over 300 members."
7
```

## Task 2: Left Right Sum Diff

### Task

You are given an array of integers, `@ints`.

Write a script to return left right sum diff array as shown below:

```perl
@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 | )

```

### My solution

This is a curly task. In the task text, there are five inputs and four outputs. In the three provided examples the number of inputs and outputs are the same.

Therefore, I've pretty much ignored the task as it is written, and instead worked based on the three examples provided.

And with that in mind it becomes a straight forward task. The first is to calculate the `left` and `right` lists.

```python
l = len(n)
left = [sum(n[0:i]) for i in range(l)]
right = [sum(n[i+1:l]) for i in range(l)]
```

And then calculate the absolute difference between the two lists.

```python
solution = [abs(left[i] - right[i]) for i in range(l)]
```

Yes, this could be done in a single function, but that point of these challenges isn't also to write the most efficient code, but to explain the workings.

The Perl solution is a little more clunkier, as there is no simple way to express an empty array as a subset the original array. For the Perl solution, I'm manually prepending 0 to the `@left` array and appending it to the `@right` array.

```perl
my $l     = scalar(@n);
my @left  = ( 0, map { sum( @n[ 0 .. $_ ] ) } ( 0 .. $l - 2 ) );
my @right = ( map( { sum( @n[ $_ .. $l - 1 ] ) } ( 1 .. $l - 1 ) ), 0 );
```

### Examples

```bash
$ ./ch-2.py 10 4 8 3
15, 1, 11, 22

$ ./ch-2.py 1
0

$ ./ch-2.py 1 2 3 4 5
14, 11, 6, 1, 10
```