Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #328 (“Replace All ?” and “Good 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 for the week of 2025-06-30 through 2025-07-06 is #328
The tasks for challenge #328 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 328-1: Replace all ? Submitted by: Mohammad Sajid Anwar You are given a string containing only lower case English letters and "?". Write a script to replace all "?" in the given string so that the string doesn’t contain consecutive repeating characters. Example 1 Input: $str = "a?z" Output: "abz" There can be many strings, one of them is "abz". The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'. Example 2 Input: $str = "pe?k" Output: "peak" Example 3 Input: $str = "gra?te" Output: "grabte"
No set of replacements of ?s is guaranteed to give a string with no consecutive identical characters, because the string may already have consecutive identical characters. All we can do is insure that our replacements don't add more.

Robbie Hatley's Perl Solution to The Weekly Challenge 328-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 328-2: Good String Submitted by: Mohammad Sajid Anwar You are given a string made up of lower and upper case English letters only. Write a script to return the good string of the given string. A string is called good string if it doesn’t have two adjacent same characters, one in upper case and other is lower case. To be explicit, you can only remove a pair if they are same characters, one in lower case and other in upper case; order is not important. Example 1 Input: $str = "WeEeekly" Output: "Weekly" We can remove either, "eE" or "Ee" to make it good. Example 2 Input: $str = "abBAdD" Output: "" We remove "bB" first: "aAdD" Then we remove "aA": "dD" Finally remove "dD". Example 3 Input: $str = "abc" Output: "abc"
To solve this problem, I'll use a 3-part index loop to find-and-remove all "bad pairs" of characters, with double index back-tracking after every deletion to prevent skipping any character pairs. (Single backtracking won't work, because removing a pair of characters may change the relationship between previous and next characters.

Robbie Hatley's Perl Solution to The Weekly Challenge 328-2
That's it for challenge 328; see you on challenge 329!
Comments
Post a Comment