Robbie Hatley's Solutions To The Weekly Challenge #255

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

This week (2024-02-04 through 2024-02-10) is weekly challenge #255. Its tasks are as follows:

Task 255-1: Odd Character
Submitted by: Mohammad Sajid Anwar

You are given two strings, $s and $t. The string $t is generated
using the shuffled characters of the string $s with an
additional character. Write a script to find the additional
character in the string $t.

Example 1:
Input: $s = "Perl" $t = "Preel"
Output: "e"

Example 2:
Input: $s = "Weekly" $t = "Weeakly"
Output: "a"

Example 3:
Input: $s = "Box" $t = "Boxy"
Output: "y"

Since this problem speaks of "characters" instead of "letters", I'll consider "a" and "A" to be different "characters", and use a case-sensitive approach. I'll attack this problem by writing a sub which first splits $s and $t into arrays @s and @t of single characters, then for each character of @s, if that character exists in @t, splices-out the first occurrence only of that character from @t, then returns @t, which should now consist of all "additional" characters (if any) which are in $t but not $s. Note that this approach doesn't care if any of the characters of $s are actually in $t; if given $s="migrant" and $t="buck", the sub will return ('b','u','c','k') because all of those letters were "added" to "migrant". (The fact that the letters ('m','i','g','r','a','n','t') were also REMOVED is irrelevant and hence ignored.)

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

Task 255-2: Most Frequent Word
Submitted by: Mohammad Sajid Anwar

You are given a paragraph $p and a banned word $w. Write a script
to return the most frequent word that is not banned.

Example 1:
Input:
   $p = "Joe hit a ball, the hit ball flew far after it was hit."
   $w = "hit"
Output: "ball"
The banned word "hit" occurs 3 times.
The other word "ball" occurs 2 times.

Example 2

Input:
   $p = "Perl and Raku belong to the same family. Perl is the ".
        "most popular language in the weekly challenge."
   $w = "the"
Output: "Perl"
The banned word "the" occurs 3 times.
The other word "Perl" occurs 2 times.

I'll write a sub that first splits each paragraph into words (/[a-zA-Z-]+/), then pushes each non-banned word to an array called "@words", then makes a frequency hash %freq from @words, then returns the most-frequent words with their frequency.

Caveat: This approach will not always handle capitalization and compound words correctly. For example, it fails to realize that the substrings "Programming" and "programming" are the same word in the following sentence: "Programming is a science, but programming is also an art." Lower-casing everything won't work either, because while "Perl" is a name, "perl" is not a word. Also, while "clean-cut" is considered one word (correct; it's a hyphenated compound), "race baiting" is considered two words (WRONG; it's a single open-compound word). Oh well; I don't have a month to spend perfecting this, so it will have to do.

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

That's it for challenge 255; see you on challenge 256!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239