Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #390 (“Decode String” and “Order Characters”)
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-09-14 through 2026-09-20 is #390.
The tasks for challenge #390 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 390-1: Decode String Submitted by: Mohammad Sajid Anwar You are given an encoded string. Write a script to return the decoded string of the given encoded string. The encoding rule is: K[encoded_string], where the encoded_string inside the square brackets is repeated exactly K > 0 times.
I'll use a Perl s/// operator in a while loop to decode innermost bracket pairs for as long as some exist, thus solving the problem from the inside out.
Robbie Hatley's Perl Solution to The Weekly Challenge 390-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 390-2: Order Characters Submitted by: Mohammad Sajid Anwar You are given a string $s (containing only alphabetic characters) and an integer $k > 0. Write a script to choose one of the first $k letters of given string and append it at the end of the string. You keep doing this until you have lexicographically smallest string and return the string.
I note that this problem is really two separate problems pasted together:
If $k is 1, then the ONLY strings we can make are the length($s) rotations of $s.
If $k is > 1, then any two adjacent letters can be swapped. If any two adjacent letters can be swapped, then any letter can be shuffled to any index. If any letter can be shuffled to any index, then all possible permutations can be created. If all possible permutations can be created, then the lexicographically-smallest word which can be created is always join(sort(split(word))).
Robbie Hatley's Perl Solution to The Weekly Challenge 390-2
That's it for challenge 390; see you on challenge 391!
Comments
Post a Comment