17 April 2025 | Challenge 317 |
Philonyms
Task 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
Solution
As stated in the task description:
use strict;
use warnings;
use experimental 'signatures';
sub acronyms ($word, @array) {
$word eq join '', map /(.)/, @array;
}
See the full solution to task 1.
Task 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 = "desc", $str2 = "dsec"
Output: true
Example 2
Input: $str1 = "fuck", $str2 = "fcuk"
Output: true
Example 3
Input: $str1 = "poo", $str2 = "eop"
Output: false
Example 4
Input: $str1 = "stripe", $str2 = "sprite"
Output: true
Solution
Pick a pair of characters from the first string and try to match the second string with the characters swapped.
Once again assuming the strings do not contain newline
characters.
There is an interesting edge case: A string is self-friendly, if it contains at least one non-unique character.
use strict;
use warnings;
use experimental 'signatures';
sub friendly ($str1, $str2) {
"$str1\n$str2" =~ /^(.*)(.)(.*)(.)(.*+)\n\1\4\3\2\5$/;
}
See the full solution to task 2.
If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.