Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #355 (“Thousand Separator” and “Mountain Array”)

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-01-05 through 2026-01-11 is #355. The tasks for challenge #355 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 355-1: Thousand Separator
Submitted by: Mohammad Sajid Anwar
You are given a positive integer, $int. Write a script to add
thousand separator, "," and return as string.

Example #1:
Input: $int = 123
Output: "123"

Example #2:
Input: $int = 1234
Output: "1,234"

Example #3:
Input: $int = 1000000
Output: "1,000,000"

Example #4:
Input: $int = 1
Output: "1"

Example #5:
Input: $int = 12345
Output: "12,345"

I'll write a subroutine which splits each integer to its decimal digits, then pops each digit from the right end of the array and appends it to the left end of an output string, first appending "," if the index is positive and 0 modulo 3. Then just return the output string.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 355-2: Mountain Array
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to
return true if the given array is a valid mountain array.
An array is mountain if and only if:
1) arr.length >= 3
and
2) There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1]     < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Example #1:
Input: @ints = (1, 2, 3, 4, 5)
Output: false

Example #2:
Input: @ints = (0, 2, 4, 6, 4, 2, 0)
Output: true

Example #3:
Input: @ints = (5, 4, 3, 2, 1)
Output: false

Example #4:
Input: @ints = (1, 3, 5, 5, 4, 2)
Output: false

Example #5:
Input: @ints = (1, 3, 2)
Output: true

I'll make these four subroutines:

   # Return the index of the first greatest element of a list:
   sub peak_idx ( @list ) {...}

   # Return 1 iff a list is strictly increasing, else return 0:
   sub is_strictly_increasing ( @list ) {...}

   # Return 1 iff a list is strictly decreasing, else return 0:
   sub is_strictly_decreasing ( @list ) {...}

   # Return 'True.' iff a list is "mountainous", else return 'False.':
   sub is_mountain ( @list ) {...}

That should be sufficient to crack the problem wide open.

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

That's it for challenge 355; see you on challenge 356!

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