Perl Weekly Challenge 269: Distribute Elements

These are some answers to the Week 269, Task 2, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on May 19, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Distribute Elements

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

Write a script to distribute the elements as described below:

1) Put the 1st element of the given array to a new array @arr1. 2) Put the 2nd element of the given array to a new array @arr2.

Once you have one element in each arrays, @arr1 and @arr2, then follow the rule below:

If the last element of the array @arr1 is greater than the last element of the array @arr2 then add the first element of the given array to @arr1 otherwise to the array @arr2.

When done distribution, return the concatenated arrays. @arr1 and @arr2.

Example 1

Input: @ints = (2, 1, 3, 4, 5)
Output: (2, 3, 4, 5, 1)

1st operation:
Add 1 to @arr1 = (2)

2nd operation:
Add 2 to @arr2 = (1)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (2, 3).

4th operation:
Again the last element of @arr1 is greate than the last element
of @arr2, add 4 to @arr1 = (2, 3, 4)

5th operation:
Finally, the last element of @arr1 is again greater than the last
element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)

Now we have two arrays:
@arr1 = (2, 3, 4, 5)
@arr2 = (1)

Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).

Example 2

Input: @ints = (3, 2, 4)
Output: (3, 4, 2)

1st operation:
Add 1 to @arr1 = (3)

2nd operation:
Add 2 to @arr2 = (2)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 4 to @arr1 = (3, 4).

Now we have two arrays:
@arr1 = (3, 4)
@arr2 = (2)

Concatenate the two arrays and return the final array: (3, 4, 2).

Example 3

Input: @ints = (5, 4, 3 ,8)
Output: (5, 3, 4, 8)

1st operation:
Add 1 to @arr1 = (5)

2nd operation:
Add 2 to @arr2 = (4)

3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (5, 3).

4th operation:
Again the last element of @arr2 is greate than the last element
of @arr1, add 8 to @arr2 = (4, 8)

Now we have two arrays:
@arr1 = (5, 3)
@arr2 = (4, 8)

Concatenate the two arrays and return the final array: (5, 3, 4, 8).

Distribute Elements in Raku

We can hardly do anything else than just follow the procedure described in the task specification.

sub distribute-elements (@in is copy) {
    my @arr1 = shift @in;
    my @arr2 = shift @in;
    for @in -> $item {
        if @arr1[*-1] > @arr2[*-1] {
            push @arr1, $item;
        } else {
            push @arr2, $item;
        }
    }
    return (@arr1, @arr2).flat;
}
my @tests = <2 1 3 4 5>, <3 2 4>, <5 4 3 8>;
for @tests -> @test {
    printf "%-10s => ", "@test[]";
    say distribute-elements @test;
}

This program displays the following output:

$ raku ./distribute-elements.raku
2 1 3 4 5  => (2 3 4 5 1)
3 2 4      => (3 4 2)
5 4 3 8    => (5 3 4 8)

Distribute Elements in Perl

This is a port to Perl of the above Raku program.

use strict;
use warnings;
use feature 'say';

sub distribute_elements {
    my @arr1 = shift;
    my @arr2 = shift;
    for my $item (@_) {
        if ($arr1[-1] > $arr2[-1]) {
            push @arr1, $item;
        } else {
            push @arr2, $item;
        }
    }
    return "@arr1 @arr2";
}
my @tests = ( [<2 1 3 4 5>], [<3 2 4>], [<5 4 3 8>] );
for my $test (@tests) {
    printf "%-10s => ", "@$test";
    say distribute_elements @$test;
}

This program displays the following output:

$ perl ./distribute-elements.pl
2 1 3 4 5  => 2 3 4 5 1
3 2 4      => 3 4 2
5 4 3 8    => 5 3 4 8

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on May 26, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.