Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #359 (“Digital Root” and “String Reduction”)

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 2026-02-02 through 2026-02-08 is #359. The tasks for challenge #359 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 359-1: Digital Root
Submitted by: Mohammad Sajid Anwar
You are given a positive integer, $int. Write a function that
calculates the additive persistence of a positive integer and
also return the digital root. Digital root is the recursive sum
of all digits in a number until a single digit is obtained.
Additive persistence is the number of times you need to sum the
digits to reach a single digit.

Example #1:
Input: $int = 38
Expected output: Digital Root = 2; Persistence = 2

Example #2:
Input: $int = 7
Expected output: Digital Root = 7; Persistence = 0

Example #3:
Input: $int = 999
Expected output: Digital Root = 9; Persistence = 2

Example #4:
Input: $int = 1999999999
Expected output: Digital Root = 1; Persistence = 3

Example #5:
Input: $int = 101010
Expected output: Digital Root = 3; Persistence = 1

This is just a matter of performing the calculations described and seeing what one gets.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 359-2: String Reduction
Submitted by: Mohammad Sajid Anwar
You are given a word containing only alphabetical characters
(/^[a-zA-Z]$/). Write a function that repeatedly removes adjacent
duplicate characters from a string until no adjacent duplicates
remain and return the final word.

Example #1:
Input: $word = "aabbccdd"
Expected output: ""

Example #2:
Input: $word = "abccba"
Expected output: ""

Example #3:
Input: $word = "abcdef"
Expected output: "abcdef"

Example #4:
Input: $word = "aabbaeaccdd"
Expected output: "aea"

Example #5:
Input: $word = "mississippi"
Expected output: "m"

Instead of using iterations, I'll use the method of backtracking: each time I remove a pair, I'll shift my index one left of where the first element of the pair was, to see if the pair removal resulted in the creation of another pair.

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

That's it for challenge 359; see you on challenge 360!

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