DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 225

Each week Mohammad S. Anwar sends out The Weekly Challenge, 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, My solutions

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 method to replace spaces with spaces. tr returns the number of occurrences found.

Examples

$ ./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
Enter fullscreen mode Exit fullscreen mode

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:

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

Enter fullscreen mode Exit fullscreen mode

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.

l = len(n)
left = [sum(n[0:i]) for i in range(l)]
right = [sum(n[i+1:l]) for i in range(l)]
Enter fullscreen mode Exit fullscreen mode

And then calculate the absolute difference between the two lists.

solution = [abs(left[i] - right[i]) for i in range(l)]
Enter fullscreen mode Exit fullscreen mode

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.

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 );
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)