Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #370 (“Popular Word” and “Scramble String”)
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
The Weekly Challenge for the week of 2026-04-20 through 2026-04-26 is #370. The tasks for challenge #370 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 370-1: Popular Word
Submitted by: Mohammad Sajid Anwar
You are given a string paragraph and an array of banned words.
Write a script to return the most popular word that is not
banned. It is guaranteed there is at least one word that is not
banned and the answer is unique. The words in paragraph are
case-insensitive and the answer should be in lowercase. The words
cannot contain punctuation symbols.
Example #1:
Inputs:
[
"Bob hit a ball, the hit BALL flew far after it was hit.",
["hit"],
],
Output: "ball"
After removing punctuation and converting to lowercase, the word
"hit" appears 3 times, and "ball" appears 2 times. Since "hit" is
on the banned list, we ignore it.
Example #2:
Inputs:
[
"Apple? apple! Apple, pear, orange, pear, apple, orange.",
["apple", "pear"],
],
Output: "orange"
"apple" appears 4 times.
"pear" appears 2 times.
"orange" appears 2 times.
"apple" and "pear" are both banned.
Even though "orange" has the same frequency as "pear", it is the
only non-banned word with the highest frequency.
Example #3:
Inputs:
[
"A. a, a! A. B. b. b.",
["b"],
],
Output: "a"
"a" appears 4 times.
"b" appears 3 times.
The input has mixed casing and heavy punctuation.
The normalised, "a" is the clear winner, since "b" is banned, "a"
is the only choice.
Example #4:
Inputs:
[
"Ball.ball,ball:apple!apple.banana",
["ball"],
],
Output: "apple"
Here the punctuation acts as a delimiter.
"ball" appears 3 times.
"apple" appears 2 times.
"banana" appears 1 time.
Example #5:
Inputs:
[
"The dog chased the cat, but the dog was faster than the cat.",
["the", "dog"],
],
Output: "cat"
"the" appears 4 times.
"dog" appears 2 times.
"cat" appears 2 times.
"chased", "but", "was", "faster", "than" appear 1 time each.
"the" is the most frequent but is banned.
"dog" is the next most frequent but is also banned.
The next most frequent non-banned word is "cat".
The problem description asserts that 3 guarantees exist:
- That at least one non-banned word exists in the paragraph.
- That exactly one answer exists.
- That the words cannot contain punctuation symbols.
But since the problem description fails to specify how those guarantees are to be enforced, I will ignore them and simply return a list of most-popular non-banned words. That list may be of size 0, 1, or more-than-1. The words may or may-not contain "punctuation symbols" (eg, "most-popular", "chuột"). To determine popularity, I'll use a hash of counts of case-folds of all non-banned words found. To provide case-insensitivity, I'll apply "fc" both to paragraph words and to banned words before each comparison.
Robbie Hatley's Perl Solution to The Weekly Challenge 370-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 370-2: Scramble String Submitted by: Roger Bell_West You are given two strings A and B of the same length. Write a script to return true if string B is a "scramble" of string A otherwise return false. String B is a "scramble" of string A if A can be transformed into B by a single (recursive) scramble operation. A scramble operation is: - If string consists of only one character, return string. - Divide the string X into two non-empty parts. - Optionally, exchange the order of those parts. - Optionally, scramble each of those parts. - Concatenate the scrambled parts to return a single string. Example #1: Input: $str1 = "abc", $str2 = "acb" Output: true "abc" split: ["a", "bc"] split: ["a", ["b", "c"]] swap: ["a", ["c", "b"]] concatenate: "acb" Example #2: Input: $str1 = "abcd", $str2 = "cdba" Output: true "abcd" split: ["ab", "cd"] swap: ["cd", "ab"] split: ["cd", ["a", "b"]] swap: ["cd", ["b", "a"]] concatenate: "cdba" Example #3: Input: $str1 = "hello", $str2 = "hiiii" Output: false A fundamental rule of scrambled strings is that they must be anagrams. Example #4: Input: $str1 = "ateer", $str2 = "eater" Output: true "ateer" split: ["ate", "er"] split: [["at", "e"], "er"] swap: [["e", "at"], "er"] concatenate: "eater" Example #5: Input: $str1 = "abcd", $str2 = "bdac" Output: false
I know there are clever ways of solving this, but I'm not capable of wrapping my head around them, so I'll use a simpler way (albeit with higher Big-O Complexity): I'll make an array of all "scrambles" of $str1 and return true iff $str2 is in that array. With that in mind, I'll make these two subroutines:
- sub scrambles ( $s ); # What are all the scrambles of a given string?
- sub is_scramble ( $s , $t ); # Is a given second string a scramble of a given first string?
Robbie Hatley's Perl Solution to The Weekly Challenge 370-2
That's it for challenge 370; see you on challenge 371!
Comments
Post a Comment