Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)
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 for the week of 2025-04-14 through 2025-04-20 is #317
The tasks for challenge #317 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 317-1: Acronyms Submitted by: Mohammad Sajid Anwar You are given an array of words and a word. Write a script to return true if concatenating the first letter of each word in the given array matches the given word, return false otherwise. Example #1: Input: @array = ("Perl", "Weekly", "Challenge") $word = "PWC" Output: true Example #2: Input: @array = ("Bob", "Charlie", "Joe") $word = "BCJ" Output: true Example #3: Input: @array = ("Morning", "Good") $word = "MM" Output: false
I'll take the approach of mapping substrings of first chars, joining, and comparing to the word.

Robbie Hatley's Perl Solution to The Weekly Challenge 317-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 317-2: Friendly Strings Submitted by: Mohammad Sajid Anwar You are given two strings. Write a script to return true if swapping any two letters in one string match the other string, return false otherwise. Example #1: Input: $str1 = "desk", $str2 = "dsek" Output: true Example #2: Input: $str1 = "lamp", $str2 = "lmap" Output: true Example 3 Input: $str1 = "roo", $str2 = "ore" Output: false Example 4 Input: $str1 = "stripe", $str2 = "sprite" Output: true
I didn't like the examples, so I substituted my own. I'll take the approach of first returning false unless the two strings are equal length, then making an array of subarrays of differences and returning false unless the array is size 2, then returning false unless one subarray is the reverse of the other. Then if I haven't returned false, I'll return true.

Robbie Hatley's Perl Solution to The Weekly Challenge 317-2
That's it for challenge 317; see you on challenge 318!
Comments
Post a Comment