Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #316 (Theme: “Sequences”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of taks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-04-07 through 2025-04-13 is #316.

The tasks for challenge #316 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 316-1: Circular
Submitted by: Mohammad Sajid Anwar
You are given a list of words. Write a script to find out
whether the last character of each word is the first character
of the following word.

Example #1:
Input: @list = ("perl", "loves", "scala")
Output: true

Example #2:
Input: @list = ("love", "the", "programming")
Output: false

Example #3:
Input: @list = ("java", "awk", "kotlin", "node.js")
Output: true

I find the title puzzling, because examples 1 and 3 give output as being "true" even though they're not circular (the last letter of "scala" is not the first letter of "perl", and the last letter fo "node.js" is not the first letter of "java"). I think a better name for the targeted attribute would be "linked". Checking for this is just a matter of using substrings in a foreach loop.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 316-2: Subsequence
Submitted by: Mohammad Sajid Anwar
You are given two strings. Write a script to find out if one
string is a subsequence of another. (A "subsequence" of a string
is a new string that is formed from the original string by
deleting some (can be none) of the characters without disturbing
the relative positions of the remaining characters.

Example #1:
Input: $str1 = "uvw", $str2 = "bcudvew"
Output: true

Example #2:
Input: $str1 = "aec", $str2 = "abcde"
Output: false

Example #3:
Input: $str1 = "sip", $str2 = "javascript"
Output: true

Clearly, a string S1 can't be a subsequence of a string S2 if S1 is longer than S2. So I'll start by sorting the two input strings by length and putting the shorter in "$shrt" and the longer in "$long. Then starting from the left, for each character of $long that's not equal to the character at the same index in $shrt, I'll delete that character from long. When finished doing that, if $long is now equal to $shrt, then $shrt was a subsequence of $long; otherwise, it wasn't.

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

That's it for challenge 316; see you on challenge 317!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #318 (“Group Positions” and “Reverse Equals”)

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