Robbie Hatley's Solutions To The Weekly Challenge #253

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

The Weekly Challenge

This week (2024-01-21 through 2024-01-27) is weekly challenge #253. Its tasks are as follows:

Task 253-1: Split Strings
Submitted by: Mohammad S Anwar
You are given an array of strings and a character separator.
Write a script to return all words separated by the given
character excluding empty string.

Example 1:
Input: @words = ("one.two.three","four.five","six")
       $separator = "."
Output: "one","two","three","four","five","six"

Example 2:
Input: @words = ("$perl$$", "$$raku$")
       $separator = "$"
Output: "perl","raku"

This is just a matter of splitting strings based on the given separator, then dumping the empty strings. One complicating factor, though, is that for separators such as the "." and "$" given in the examples to actually work, they must be stripped of their magical "meta" powers by using the "\Q" de-meta operator. So something like this sub should work:

sub split_strings ($separator, @array) {
   return grep {length($_)>0} map {split /\Q$separator\E/, $_} @array;
}

Robbie Hatley's Perl Solution to The Weekly Challenge 253-1

Task 253-2: Weakest Row
Submitted by: Mohammad S Anwar
You are given an m x n binary matrix i.e. only 0 and 1 where 1
always appear before 0. A row i is weaker than a row j if one
of the following is true:
a) The number of 1s in row i is less than
   the number of 1s in row j.
b) Both rows have the same number of 1 and i < j.
Write a script to return the order of rows from weakest to
strongest.

Example 1:
Input: $matrix = [
                   [1, 1, 0, 0, 0],
                   [1, 1, 1, 1, 0],
                   [1, 0, 0, 0, 0],
                   [1, 1, 0, 0, 0],
                   [1, 1, 1, 1, 1]
                 ]
Output: (2, 0, 3, 1, 4)
The number of 1s in each row is:
- Row 0: 2
- Row 1: 4
- Row 2: 1
- Row 3: 2
- Row 4: 5

Example 2:
Input: $matrix = [
                   [1, 0, 0, 0],
                   [1, 1, 1, 1],
                   [1, 0, 0, 0],
                   [1, 0, 0, 0]
                 ]
Output: (0, 2, 3, 1)
The number of 1s in each row is:
- Row 0: 1
- Row 1: 4
- Row 2: 1
- Row 3: 1

A combined sort can be indicated by PrimarySort || SecondarySort. In this case, the primary sort is by "# of 1s" and secondary sort is by index. The "# of 1s" is just sum0(Row), and the indices are just 0..$#$aref. So this subroutine should do the trick:

sub weakest ($aref) {
   return sort {sum0(@{$$aref[$a]})<=>sum0(@{$$aref[$b]}) || $a<=>$b} 0..$#$aref;
}

Robbie Hatley's Perl Solution to The Weekly Challenge 253-2

That's it for challenge 253; see you on challenge 254!

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