Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #314 (Theme: "Destroy Unwanted Columns")

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

The Weekly Challenge for the week of 2025-03-24 through 2025-03-30 is #314.

The tasks for challenge #314 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 314-1: Equal Strings
Submitted by: Mohammad Sajid Anwar
You are given three strings. You are allowed to remove the
rightmost character of a string to make all equals. Write a
script to return the number of operations to make it equal,
otherwise -1.

Example #1:
Input: $s1 = "abc", $s2 = "abb", $s3 = "ab"
Output: 2
Operation 1: Delete "c" from the string "abc"
Operation 2: Delete "b" from the string "abb"

Example #2:
Input: $s1 = "ayz", $s2 = "cyz", $s3 = "xyz"
Output: -1

Example #3:
Input: $s1 = "yza", $s2 = "yzb", $s3 = "yzc"
Output: 3

The first thing I note is that "-1" will be returned if-and-only-if the 3 input strings do NOT all start with the same first character. (The examples rule-out the idea that 3 empty strings should be considered "equal".) With that in-mind, I see two main ways to attack this:

  1. Nibble from the right, chopping-off substrings until the 3 strings are equal, and count operations.
  2. Count triplets of equal characters from the left, and subtract number of equal characters from total.

Either will give the same answer. I'll go with option 2, because it gives an easy way to determine when -1 should be returned: precisely when the number of equal characters (counted in triplets from the left) is 0 (or if any of the three strings is empty). With that in-mind, I'll make these 2 subroutines:

  1. col($pref,$n) (Return a column.)
  2. del_unequ_chrs($pref) (Delete unequal characters.)

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 314-2: Sort Column
Submitted by: Mohammad Sajid Anwar
You are given a list of strings of same length. Write a script
to make each column sorted lexicographically by deleting any
non-sorted columns. Return the total columns deleted.

Example #1:
Input: @list = ("swpc", "tyad", "azbe")
Output: 2
swpc
tyad
azbe
Column 1: "s", "t", "a" => non sorted
Column 2: "w", "y", "z" => sorted
Column 3: "p", "a", "b" => non sorted
Column 4: "c", "d", "e" => sorted
Total columns to delete to make it sorted lexicographically: 2

Example #2:
Input: @list = ("cba", "daf", "ghi")
Output: 1

Example #3:
Input: @list = ("a", "b", "c")
Output: 0

I could cheat and just return the number of columns necessary to be deleted, but that's NOT what the problem description actually says; it says "make each column sorted lexicographically by DELETING any non-sorted columns". So I'll actually do that. I'll make these 4 subroutines:

  1. col($pref,$n) (Return a column.)
  2. del_col($pref,$n) (Delete a column.)
  3. is_srt(@list) (Is a list sorted?)
  4. del_unsrt_cols($pref) (Delete unsorted columns.)

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

That's it for challenge 314; see you on challenge 315!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #273

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #313 ("Broken Keys" and "Reverse Letters")

Robbie Hatley's Solutions To The Weekly Challenge #267