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

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 2025-10-20 through 2025-10-26 is #344. The tasks for challenge #344 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 344-1: Array Form Compute
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints and an integer, $x.
Write a script to add $x to the integer in the array-form.
The array form of an integer is a digit-by-digit representation
stored as an array, where the most significant digit is at the
0th index.

Example 1
Input: @ints = (1, 2, 3, 4), $x = 12
Output: (1, 2, 4, 6)

Example 2
Input: @ints = (2, 7, 4), $x = 181
Output: (4, 5, 5)

Example 3
Input: @ints = (9, 9, 9), $x = 1
Output: (1, 0, 0, 0)

Example 4
Input: @ints = (1, 0, 0, 0, 0), $x = 9999
Output: (1, 9, 9, 9, 9)

Example 5
Input: @ints = (0), $x = 1000
Output: (1, 0, 0, 0)

This is just split(//, join('', @ints)+$x).

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 344-2: Array Formation
Submitted by: Mohammad Sajid Anwar
You are given two lists: @source and @target. Write a script to
see if you can build the exact @target by putting these smaller
lists from @source together in some order. You cannot break apart
or change the order inside any of the smaller lists in @source.

Example 1
Input: @source = ([2,3], [1], [4])
       @target = (1, 2, 3, 4)
Output: true
Use in the order: [1], [2,3], [4]

Example 2
Input: @source = ([1,3], [2,4])
       @target = (1, 2, 3, 4)
Output: false

Example 3
Input: @source = ([9,1], [5,8], [2])
       @target = (5, 8, 2, 9, 1)
Output: true
Use in the order: [5,8], [2], [9,1]

Example 4
Input: @source = ([1], [3])
       @target = (1, 2, 3)
Output: false
Missing number: 2

Example 5
Input: @source = ([7,4,6])
       @target = (7, 4, 6)
Output: true
Use in the order: [7, 4, 6]

I'll use a recursive approach. I'll first try to match each source sub-array to the first few digits of the target. If no match, move to next source sub-array. If source sub-array matches target exactly, return 'true'. If partial match, send unmatched portions of source and target to next recursive level, and if the recursive call returns 'true', return 'true'. If no recursive call returns true, then return 'false'.

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

That's it for challenge 344; see you on challenge 345!

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