Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #315 (Theme: “If I Had Words”)
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 for the week of 2025-03-31 through 2025-04-06 is #315.
“If I had words
To make a day for you
I sing you a morning
golden and new
“I would make this day
Last for all time
Give you a night
Deep in moonshine”
~~Johnathan Hodge
The tasks for challenge #315 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 315-1: Find Words
Submitted by: Mohammad Sajid Anwar
You are given a list of words and a character. Write a script to
return the index of word in the list where you find the given
character.
Example #1:
Input: @list = ("the", "weekly", "challenge")
$char = "e"
Output: (0, 1, 2)
Example #2:
Input: @list = ("perl", "raku", "python")
$char = "p"
Output: (0, 2)
Example #3:
Input: @list = ("abc", "def", "bbb", "bcd")
$char = "b"
Output: (0, 2, 3)
Since this problem involves finding indexes matching a criteria, I'll use the "indexes" function from CPAN module "List::MoreUtils" to apply regular-expression "m/$char/" to all words in @list.
Robbie Hatley's Perl Solution to The Weekly Challenge 315-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 315-2: Find Third
Submitted by: Mohammad Sajid Anwar
You are given a sentence and two words. Write a script to return
all words in the given sentence that appear in sequence to the
given two words.
Example #1:
Input:
$sentence = "Perl is a my favourite language but Python is my favourite too."
$first = "my"
$second = "favourite"
Output:
("language", "too")
Example #2:
Input:
$sentence = "Barbie is a beautiful doll also also a beautiful princess."
$first = "a"
$second = "beautiful"
Output:
("doll", "princess")
Example #3:
Input:
$sentence = "we will we will rock you rock you.",
$first = "we"
$second = "will"
Output:
("we", "rock")
Since this task is all about "finding words which follow after a first and second word in-sequence", I'll take the approach of first spliting $sentence into an array @w of words, then greping the indices after 0 and 1 for indices such that $w[$_-2] is "first" word and $w[$_-1] is "second word".
Robbie Hatley's Perl Solution to The Weekly Challenge 315-2
That's it for challenge 315; see you on challenge 316!
Comments
Post a Comment