Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #353 (“Max Words” and “Validate Coupon”)

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 2025-12-22 through 2025-12-28 is #353. The tasks for challenge #353 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 353-1: Max Words
Submitted by: Mohammad Sajid Anwar
You are given an array of sentences. Write a script to return
the maximum number of words that appear in a single sentence.

Example #1:
Input:  ("Hello world",
         "This is a test",
         "Perl is great")
Output: 4

Example #2:
Input:  ("Single")
Output: 1

Example #3:
Input:  ("Short",
         "This sentence has six words in total",
         "A B C",
         "Just four words here")
Output: 7 (The second sentence lied.)

Example #4:
Input:  ("One",
         "Two parts",
         "Three part phrase",
         "")
Output: 3

Example #5:
Input: ("The quick brown fox jumps over the lazy dog",
        "A",
        "She sells seashells by the seashore",
        "To be or not to be that is the question")
Output: 10

I'll use a "m/\S+/g" operator, with the output assigned to an array to give it "list context", to get the list of "words" (defined as clusters of non-space characters), scalar that to get the count, and update a "max" variable if the count is greater than "max", then simply return "max".

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 353-2: Validate Coupon
Submitted by: Mohammad Sajid Anwar
You are given three arrays: @codes, @names and @status.
Write a script to validate codes in the given array. A code is
valid when the following conditions are true:
- codes[i] is non-empty and consists only of alphanumeric
  characters (a-z, A-Z, 0-9) and underscores (_).
- names[i] is one of the following four categories:
  "electronics", "grocery", "pharmacy", "restaurant".
- status[i] is true.
Return an array of booleans indicating validity: output[i] is
true if and only if codes[i], names[i] and status[i] are all
valid.

Example #1:
Input: @codes  = ("A123", "B_456", "C789", "D@1", "E123")
       @names  = ("electronics", "restaurant", "electronics",
                  "pharmacy", "grocery")
       @status = ("true", "false", "true", "true", "true")
Expected output: (true, false, true, false, true)

Example #2:
Input: @codes  = ("Z_9", "AB_12", "G01", "X99", "test")
       @names  = ("pharmacy", "electronics", "grocery",
                  "electronics", "unknown")
       @status = ("true", "true", "false", "true", "true")
Expected output: (true, true, false, true, false)

Example #3:
Input: @codes  = ("_123", "123", "", "Coupon_A", "Alpha")
       @names  = ("restaurant", "electronics", "electronics",
                  "pharmacy", "grocery")
       @status = ("true", "true", "false", "true", "true")
Expected output: (true, true, false, true, true)

Example #4:
Input: @codes  = ("ITEM_1", "ITEM_2", "ITEM_3", "ITEM_4")
       @names  = ("electronics", "electronics", "grocery",
                  "grocery")
       @status = ("true", "true", "true", "true")
Expected output: (true, true, true, true)

Example #5:
Input: @codes  = ("CAFE_X", "ELEC_100", "FOOD_1", "DRUG_A",
                  "ELEC_99")
       @names  = ("restaurant", "electronics", "grocery",
                  "pharmacy", "electronics")
       @status = ("true", "true", "true", "true", "false")
Expected output: (true, true, true, true, false)

I'll first verify that the arrays are of equal length. Then for each index, I'll consider a code/name/status trio "valid" if-and-only-if its code is well-formed, its status is valid, and its name is valid. I'll then return my array of "true" and "false" markers indicating whether each code/name/status trio is valid.

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

That's it for challenge 353; see you on challenge 354!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)