Robbie Hatley's Solutions To The Weekly Challenge #266

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-04-21 through 2024-04-27) is weekly challenge #266. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 266-1: Uncommon Words
Submitted by: Mohammad Sajid Anwar
You are given two sentences, $line1 and $line2. Write a script
to find all "uncommmon" words in any order in the given two
sentences, or return ('') if none are found. A word is
"uncommon" if it appears exactly once in one of the sentences
and doesn’t appear in other sentence.

Example 1:
Input: $line1 = 'Mango is sweet'
       $line2 = 'Mango is sour'
Output: ('sweet', 'sour')

Example 2:
Input: $line1 = 'Mango Mango'
       $line2 = 'Orange'
Output: ('Orange')

Example 3:
Input: $line1 = 'Mango is Mango'
       $line2 = 'Orange is Orange'
Output: ('')

This problem's statement is logically equivalent to saying "a word is 'uncommon' if-and-only-if it appears exactly once in two input sentences combined". I'll generalize my solution by allowing any number of input strings consisting of any valid Unicode characters. I'll consider a "word" to be the case-fold of any contiguous cluster of "letter" characters and apostrophes appearing in a string, so that if a string is "f7&B didn't QaGT", then it contains exactly 4 "words" which are "f", "b", "didn't", and "qagt". I'll then make a hash of abundances of all such "words" encountered in all input strings taken together. All words (if any) having an abundance of 1 will be considered "uncommon". Something like this should do the trick:

   use v5.36;
   sub uncommon ($aref) {
      my %a;
      for my $string (@$aref) {
         for my $word (map {fc} split /[^\pL']+/, $string) {
            ++$a{$word}; # Autovivify as necessary.
         }
      }
      # Return all uncommon (abundance == 1) words:
      return grep {1 == $a{$_}} keys %a;
   }

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 266-2: X Matrix
Submitted by: Mohammad Sajid Anwar
You are given a square matrix, $matrix. Write a script to find
if the given matrix is X Matrix. A square matrix is an X Matrix
if all the elements on the main diagonal and antidiagonal are
non-zero and everything else are zero.

Example 1:
Input: $matrix = [1, 0, 0, 2],
                 [0, 3, 4, 0],
                 [0, 5, 6, 0],
                 [7, 0, 0, 1],
Output: true

Example 2:
Input: $matrix = [1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9],
Output: false

Example 3:
Input: $matrix = [1, 0, 2],
                 [0, 3, 0],
                 [4, 0, 5],
Output: true

This is just a matter of checking the indices. If "n" is the size of the matrix, then:
The "main" diagonal is given by j = i
The "anti" diagonal is given by j = n-1-i
Just structure the matrix as an array of arrays, then test each element to make sure that they're all non-zero if on one of the diagonals, or zero otherwise.

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

That's it for challenge 266; see you on challenge 267!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262