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

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-17 through 2025-03-23 is #313.

The tasks for challenge #313 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 313-1: Broken Keys
Submitted by: Mohammad Sajid Anwar
You have a broken keyboard which sometimes type a character more
than once. You are given a string and actual typed string. Write
a script to find out if the actual typed string is meant for the
given string.

Example #1:
Input: $name = "perl", $typed = "perrrl"
Output: true
Here "r" is pressed 3 times instead of 1 time.

Example #2:
Input: $name = "raku", $typed = "rrakuuuu"
Output: true

Example #3:
Input: $name = "python", $typed = "perl"
Output: false

Example #4:
Input: $name = "coffeescript", $typed = "cofffeescccript"
Output: true

“6accdae13eff7i3l9n4o4qrr4s8t12ux”
~~Sir Isaac Newton, Knight of The British Empire and Discoverer of Calculus and Physics

No, he didn't loose his mind or become senile. That's a "signature" of a document, basically the numbers of each kind of letter which appear in the document.

And that's also the approach I'll use to solving PWCC task 313-1: I'll make "signature" arrays for the "$name" and "$typed" strings, consisting of [letter, number] pairs indicating each contiguous group of identical characters within each string. For "$typed" to match "$name", the two arrays must be of equal length, with the same sequence of characters, with the multiplicity numbers of "$typed" being greater-than-or-equal-to the corresponding multiplicity numbers of "$name".

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 313-2: Reverse Letters
Submitted by: Mohammad Sajid Anwar
You are given a string. Write a script to reverse only the
alphabetic characters in the string.

Example #1:
Input: $str = "p-er?l"
Output: "l-re?p"

Example #2:
Input: $str = "wee-k!L-y"
Output: "yLk-e!e-w"

Example #3:
Input: $str = "_c-!h_all-en!g_e"
Output: "_e-!g_nel-la!h_c"

My approach was to first "split" each string to an array of single chars, then determine which indices of the array are letters, then use "slices" to reverse the letters only, then "join" the array back to a string.

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

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

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