Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #389 (“Reorder Notes” and “ZigZag Subarray”)

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-08-31 through 2026-09-07 is #389.

The tasks for challenge #389 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 389-1: Reorder Notes
Submitted by: Reinier Maliepaard
You are given an array [composer, notes, permutation].
Reconstruct the melody by using each permutation value as the
destination position of the corresponding note. Use no explicit
for, foreach, or while loops. Output each result as
"COMPOSER => reordered notes". ASSUMPTION: Input is valid; the
notes array and permutation array have identical lengths, and
the permutation contains each position from 1 to N exactly once.

I solved this by assigning the original array to an initially-empty array "sliced" by the permutation numbers. (Slicing the original array won't work, because the permutation numbers indicate the destination indices, not the source indices.)

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 389-2: ZigZag Subarray
Submitted by: Roger Bell_West
You are given an array of integers. Write a script to find the
length of the longest contiguous subarray where the numbers
alternate between strictly increasing and strictly decreasing
(a ZigZag pattern). A sequence of numbers $A = [a0, a1, …, ak]
with length $k >= 1 is considered a ZigZag sequence iff every
adjacent pair alternates direction:
a_0 < a_1 > a_2 < a_3 > ...
OR
a_0 > a_1 < a_2 > a_3 < ...
NOTE: A single element (length 1) or any two distinct elements
(length 2) are automatically valid ZigZag sequences.

I'll simply iterate through the array, keeping track of the longest zigzag sequence seen so-far.

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

That's it for challenge 389; see you on challenge 390!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #341 (“Broken Keyboard” and “Reverse Prefix”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #344 (“Array Form Compute” and “Array Formation”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #343 (“Zero Friend” and “Champion Team”)