Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #331 (“Last Word” and “Buddy Strings”)
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:
The Weekly Challenge for the week of 2025-07-21 through 2025-07-27 is #331
The tasks for challenge #331 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 331-1: Last Word Submitted by: Mohammad Sajid Anwar You are given a string. Write a script to find the length of last word in the given string. Example #1: Input: $str = "The Weekly Challenge" Output: 9 Example #2: Input: $str = " Hello World " Output: 5 Example #3: Input: $str = "Let's begin the fun" Output: 3
There are a number of ways of approaching this, including using "split" to obtain a list of words which are in the string. But I'll use a simpler approach: I'll use an m// operator with a (capture group) to isolate the final word into "$1", then feed "$1" into the length() operator.

Robbie Hatley's Perl Solution to The Weekly Challenge 331-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 331-2: Buddy Strings Submitted by Mohammad Sajid Anwar. Edited by Robbie Hatley for family-friendliness, grammar, and clarification of examples. You are given two strings, source and target. Write a script to find out if the given strings are Buddy Strings. If swapping of two letters in one string makes it same as the other string, then they are "Buddy Strings". Example #1: Input: $source = "fram" $target = "farm" Output: true (Swapping 'a' and 'r' makes them buddy strings.) Example #2: Input: $source = "love" $target = "love" Output: false (Because the strings are identical but have no letters in-common, any swap leaves the two strings different.) Example #3: Input: $source = "fodo" $target = "food" Output: true (Because the strings are different with exactly two differences which are mirror images of each other, swapping the two "different" letters in one string makes it the same as the other string.) Example #4: Input: $source = "feed" $target = "feed" Output: true (Because the strings are identical, normally any swapping of letters in one string would make it different from the other; but because duplicate letters exist, we can swap THOSE, resulting in the two strings STILL being identical.)
I'll write a sub that determines the buddy-ness of a pair of strings by running these checks:
- If the lengths of the two strings are not equal, the strings aren't buddies.
- If the strings are identical, they're buddies if-and-only-if they contain duplicate characters.
- If the strings are same-size-but-different:
- If number of indices for which the characters are unequal is not 2, the strings aren't buddies.
- If the second pair of unequal characters is not the reverse of the first, the strings aren't buddies.
- Otherwise, the strings are buddies.

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