Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #340 (“Duplicate Removals” and “Ascending Numbers”)

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 340-1: Duplicate Removals
Submitted by: Mohammad Sajid Anwar
You are given a string consisting of lowercase English letters.
Write a script to return the final string after all duplicate
removals have been made. Repeat duplicate removals on the given
string until we no longer can. A duplicate removal consists of
choosing two adjacent and equal letters and removing them.

Example #1:
Input:  'abbaca'
Output: 'ca'
Step 1: Remove 'bb' => 'aaca'
Step 2: Remove 'aa' => 'ca'

Example #2:
Input:  'azxxzy'
Output: 'ay'
Step 1: Remove 'xx' => 'azzy'
Step 2: Remove 'zz' => 'ay'

Example #3:
Input:  'aaaaaaaa'
Output: ''
Step 1: Remove 'aa' => 'aaaaaa'
Step 2: Remove 'aa' => 'aaaa'
Step 3: Remove 'aa' => 'aa'
Step 4: Remove 'aa' => ''

Example #4:
Input:  'aabccba'
Output: 'a'
Step 1: Remove 'aa' => 'bccba'
Step 2: Remove 'cc' => 'bba'
Step 3: Remove 'bb' => 'a'

Example #5:
Input:  'abcddcba'
Output: ''
Step 1: Remove 'dd' => 'abccba'
Step 2: Remove 'cc' => 'abba'
Step 3: Remove 'bb' => 'aa'
Step 4: Remove 'aa' => ''

To solve this problem, I'll use "substr" in a 3-part loop, double-decrementing the index after each deletion in order to account for possible new "duplicate" relationship between previous character and new current character.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 340-2: Ascending Numbers
Submitted by: Mohammad Sajid Anwar

You are given a string which is a list of tokens separated by
single spaces. Every token is either a positive number consisting
of digits 0-9 with no leading zeros, or a word consisting of
lowercase English letters. Write a script to check if all the
numbers in the given string are strictly increasing from left
to right.

Example #1:
Input:  "The cat has 3 kittens 7 toys 10 beds"
Output: true
Numbers 3, 7, 10 - strictly increasing.

Example #2:
Input:  'Alice bought 5 apples 2 oranges 9 bananas'
Output: false

Example #3:
Input:  'I ran 1 mile 2 days 3 weeks 4 months'
Output: true

Example #4:
Input:  'Bob has 10 cars 10 bikes'
Output: false

Example #5:
Input:  'Zero is 0 one is 1 two is 2'
Output: true

To solve this problem, for each token $t which consists solely of digits, I'll push (0+$t) to an array @a, then return true if-and-only-if @a is strictly-increasing.

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

That's it for challenge 340; see you on challenge 341!

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