Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #329 (“Counter Integers” and “Nice 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

The Weekly Challenge for the week of 2025-07-07 through 2025-07-13 is #329

The tasks for challenge #329 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 329-1: Counter Integers
Submitted by: Mohammad Sajid Anwar
You are given a string containing only lower case English
letters and digits. Write a script to replace every non-digit
character with a space and then return all the distinct integers
left.

Example #1:
Input: $str = "the1weekly2challenge2"
Output: 1, 2
2 is appeared twice, so we count it one only.

Example #2:
Input: $str = "go21od1lu5c7k"
Output: 21, 1, 5, 7

Example #3:
Input: $str = "4p3e2r1l"
Output: 4, 3, 2, 1

I think I'll approach this by first using a regular expression in a s/// statement to change each cluster of "non-digit characters" into a single space, then split the string on spaces to a list, then use function "none" from CPAN module "List::Util" to collect integers which haven't been collected yet, then return that collection of unique integers.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 329-2: Nice 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 longest substring of
the give string which is nice. A string is nice if, for every
letter of the alphabet that the string contains, it appears both
in uppercase and lowercase.

Example #1:
Input: $str = "YaaAho"
Output: "aaA"

Example #2:
Input: $str = "cC"
Output: "cC"

Example #3:
Input: $str = "A"
Output: ""
No nice string found.

I used a structured approach. The first thing I needed was to toggle the case of a character, so I made a "tc" subroutine to do that. Next, I needed a way to determine whether a string is "nice" (as per the problem description), so I made a "is_nice" subroutine to do that (by using "none" from CPAN module "List::Util to determine which characters in a string have no opposite-case analogs in the string). And finally, I wrote a subroutine called "max_nice" which determines the first max-length "nice" substring of the original string.

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

That's it for challenge 329; see you on challenge 330!

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”)