Robbie Hatley's Perl Solutions To The Weekly Challenge #211

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

The Weekly Challenge

This week (2023-04-02 through 2023-04-08) is weekly challenge #211.

Task 1 is as follows:

You are given a matrix m x n. Write a script to find out if the given matrix is Toeplitz Matrix. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

For some reason this gave me more trouble than Task 2 below even though that one was conceptually harder. I guess I just found it annoying that I had to riffle through columns of all different lengths. My approach turned out to use... can you guess?... yep, nested 3-part loops again. :-) I do tend to use those a lot. Check it out:

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

Task 2 is as follows:

You are given an array of integers. Write a script to find out if the given can be split into two separate arrays whose average are the same.

This is mathematically quite complex as it takes a deep dive into the Hellish realms of Combinatorics, and Partitions in particular. However, I found some simplifying principles (no need to test every partition because many are duplicates) and a helpful CPAN module (Set::Partition). With the aid of those, I was able to solve it pretty quickly. This time I used a variety of loops (3-part, foreach, while) rather than relying on 3-part only as I normally do in these challenges:

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

That's it for 211; see you on 212!

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