Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #399: (“Longest Repeating Character” and “Longest Absolute Path”)

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-11-09 through 2026-11-15 is #399. The tasks for this challenge are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 399-1: Longest Repeating Character
Submitted by: Mohammad Sajid Anwar
You are given a string str and an integer k > 0. Write a script
to choose any character of str and change it to another character.
You may perform this operation up to k times. Return the length
of the longest reachable substring of a single letter.

Musical accompaniment: Paul McCartney's "Long And Winding Road".

To solve this problem, I'll generate every substring of the string, then calculate the aomac (Abundance Of Most-Abundant Character) for each substring. Then if length - aomac is less-than-or-equal-to k and length is greater than max, update max. Then return max.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 399-2: Longest Absolute Path
Submitted by: Mohammad Sajid Anwar
You are given a file system as string. Write  a script to return
the length of the longest absolute path to a file in the abstracted
file system. If there is no file in the system, return 0.

Musical accompaniment: Billy Joel's "For The Longest Time"

To solve this problem, I'll use the "number of leading tabs" as "depths", then keep track of "current working directory" by pushing/popping directories on a stack. Then make the relative path (no leading slant) for each file, and keep track of the longest. Then return longest path and its length.

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

That's it for challenge 399; see you on challenge 400!

Comments

Popular posts from this blog

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #357 (“Kaprekar Constant” and “Unique Fraction Generator”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #351 (“Special Average” and “Arithmetic Progression”)

Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #356 (“Kolakoski Sequence” and “Who Wins”)