Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #318 (“Group Positions” and “Reverse Equals”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of taks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-04-21 through 2025-04-27 is #318

The tasks for challenge #318 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 318-1: Group Position
Submitted by: Mohammad Sajid Anwar
You are given a string of lowercase letters. Write a script to
find the position of all groups in the given string. Three or
more consecutive letters form a group. Return "" if none found.

Example #1:
Input: $str = "abccccd"
Output: "cccc"

Example #2:
Input: $str = "aaabcddddeefff"
Output: "aaa", "dddd", "fff"

Example #3:
Input: $str = "abcdd"
Output: ""

I find the examples (which print group contents) to contradict the problem description (which asks for group indexes). So I'll write a sub that gives both. All groups of 3+ contiguous identical characters in a string can be found by matching the string to regexp "(.)\1{2,}" using the "m//" operator in scalar context in a while loop. Then for each match $m I'll get its index $i within the string using "index", set next offset to $i+1 (to start next index search one-past previous), and push ordered pair [$m,$i] to the output array. After all matches (if any) have been found, I'll simply return the output array.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 318-2: Reverse Equals
Submitted by: Roger Bell_West
You are given two arrays of integers, each containing the same
elements as the other. Write a script to return true if one array
can be made to equal the other by reversing exactly one
contiguous subarray.

Example #1:
Input: @source = (3, 2, 1, 4)
       @target = (1, 2, 3, 4)
Output: true
Reverse elements: 0-2

Example #2:
Input: @source = (1, 3, 4)
       @target = (4, 1, 3)
Output: false

Example #3:
Input: @source = (2)
       @target = (2)
Output: true

The only approach I can see is to laboriously reverse each subarray of array 1 and see if the altered array 1 now equals array 2.

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

That's it for challenge 318; see you on challenge 319!

Comments

Popular posts from this blog

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

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #317 (Theme: “Friendly Acronyms”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #319 (“Word Count” and “Minimum Common”)