|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +=pod |
| 4 | +
|
| 5 | +-------------------------------------------------------------------------------------------------------------- |
| 6 | +TITLE AND ATTRIBUTION: |
| 7 | +Solutions in Perl for The Weekly Challenge 344-2, |
| 8 | +written by Robbie Hatley on Sat Oct 25, 2025. |
| 9 | +
|
| 10 | +-------------------------------------------------------------------------------------------------------------- |
| 11 | +PROBLEM DESCRIPTION: |
| 12 | +Task 344-2: Array Formation |
| 13 | +Submitted by: Mohammad Sajid Anwar |
| 14 | +You are given two lists: @source and @target. Write a script to |
| 15 | +see if you can build the exact @target by putting these smaller |
| 16 | +lists from @source together in some order. You cannot break apart |
| 17 | +or change the order inside any of the smaller lists in @source. |
| 18 | +
|
| 19 | +Example 1 |
| 20 | +Input: @source = ([2,3], [1], [4]) |
| 21 | + @target = (1, 2, 3, 4) |
| 22 | +Output: true |
| 23 | +Use in the order: [1], [2,3], [4] |
| 24 | +
|
| 25 | +Example 2 |
| 26 | +Input: @source = ([1,3], [2,4]) |
| 27 | + @target = (1, 2, 3, 4) |
| 28 | +Output: false |
| 29 | +
|
| 30 | +Example 3 |
| 31 | +Input: @source = ([9,1], [5,8], [2]) |
| 32 | + @target = (5, 8, 2, 9, 1) |
| 33 | +Output: true |
| 34 | +Use in the order: [5,8], [2], [9,1] |
| 35 | +
|
| 36 | +Example 4 |
| 37 | +Input: @source = ([1], [3]) |
| 38 | + @target = (1, 2, 3) |
| 39 | +Output: false |
| 40 | +Missing number: 2 |
| 41 | +
|
| 42 | +Example 5 |
| 43 | +Input: @source = ([7,4,6]) |
| 44 | + @target = (7, 4, 6) |
| 45 | +Output: true |
| 46 | +Use in the order: [7, 4, 6] |
| 47 | +
|
| 48 | +-------------------------------------------------------------------------------------------------------------- |
| 49 | +PROBLEM NOTES: |
| 50 | +I'll use a recursive approach. I'll first try to match each source sub-array to the first few digits of the |
| 51 | +target. If no match, move to next source sub-array. If source sub-array matches target exactly, return 'true'. |
| 52 | +If partial match, send unmatched portions of source and target to next recursive level, and if the recursive |
| 53 | +call returns 'true', return 'true'. If no recursive call matches, return 'false'. |
| 54 | +
|
| 55 | +-------------------------------------------------------------------------------------------------------------- |
| 56 | +IO NOTES: |
| 57 | +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a |
| 58 | +single-quoted array of arrays of arrays of arrays of digits, in proper Perl syntax, like so: |
| 59 | +
|
| 60 | +./ch-2.pl '([[[1,7],[6,3]], [6,1,7,3]], [[[1,7],[6,3]], [6,3,1,7]])' |
| 61 | +
|
| 62 | +Of each inner array, the first element will be the "source" described in the problem description, and the |
| 63 | +second element will be the "target". |
| 64 | +
|
| 65 | +Output is to STDOUT and will be each input followed by the corresponding output. |
| 66 | +
|
| 67 | +=cut |
| 68 | + |
| 69 | +# ------------------------------------------------------------------------------------------------------------ |
| 70 | +# PRAGMAS, MODULES, AND SUBS: |
| 71 | + |
| 72 | + use v5.36; |
| 73 | + use utf8::all; |
| 74 | + |
| 75 | + # Are two arrays equal? |
| 76 | + sub is_equal ($aref1, $aref2) { |
| 77 | + my $m = scalar @$aref1; |
| 78 | + my $n = scalar @$aref2; |
| 79 | + if ($m != $n) {return 0} |
| 80 | + for (0..$n-1) {if ($$aref1[$_] != $$aref2[$_]) {return 0}} |
| 81 | + return 1} |
| 82 | + |
| 83 | + # Can a target be built from a source? |
| 84 | + sub can_build ($aref1, $aref2) { |
| 85 | + my @src = @$aref1; # Array of source arrays. |
| 86 | + my $ssz = scalar(@src); # Number of source arrays. |
| 87 | + my @tar = @$aref2; # Target array. |
| 88 | + my $tsz = scalar(@tar); # Target size. |
| 89 | + for my $idx (0..$ssz-1) { |
| 90 | + my @sa = @{$src[$idx]}; # Source array. |
| 91 | + my $sas = scalar(@sa); # Size of source array. |
| 92 | + # Skip @sa if it's bigger than @tar: |
| 93 | + next if ($sas > $tsz); |
| 94 | + # Get the first $sas elements of @tar: |
| 95 | + my @prefix = @tar[0..$sas-1]; |
| 96 | + # Skip to next source array if @sa doesn't match @prefix: |
| 97 | + next if (!is_equal(\@sa,\@prefix)); |
| 98 | + # If we get to here, sub-array matches beginning of target. |
| 99 | + # If this is a total match, return 'true': |
| 100 | + return 'true' if $sas == $tsz; |
| 101 | + # If we get to here, it's a partial match. Call this function again, |
| 102 | + # recursively, and send it the unused portions of @src and @trg: |
| 103 | + my @psrc = (@src[0..$idx-1], @src[$idx+1..$ssz-1]); |
| 104 | + my @ptrg = (@tar[$sas..$tsz-1]); |
| 105 | + my $result = can_build(\@psrc,\@ptrg); |
| 106 | + # If that recursive function call returned 'true', return 'true': |
| 107 | + return 'true' if 'true' eq $result} |
| 108 | + # If we get to here, no way exists to build @src from @tar, |
| 109 | + # so return 'false': |
| 110 | + return 'false'} |
| 111 | + |
| 112 | +# ------------------------------------------------------------------------------------------------------------ |
| 113 | +# INPUTS: |
| 114 | +my @arrays = @ARGV ? eval($ARGV[0]) : |
| 115 | +( |
| 116 | + # Example 1 input: |
| 117 | + [[[2,3], [1], [4]], [1, 2, 3, 4]], |
| 118 | + # Expected output: true |
| 119 | + |
| 120 | + # Example 2 input: |
| 121 | + [[[1,3], [2,4]], [1, 2, 3, 4]], |
| 122 | + # Expected output: false |
| 123 | + |
| 124 | + # Example 3 input: |
| 125 | + [[[9,1], [5,8], [2]], [5, 8, 2, 9, 1]], |
| 126 | + # Expected output: true |
| 127 | + |
| 128 | + # Example 4 input: |
| 129 | + [[[1], [3]], [1, 2, 3]], |
| 130 | + # Expected output: false |
| 131 | + |
| 132 | + # Example 5 input: |
| 133 | + [[[7,4,6]], [7, 4, 6]], |
| 134 | + # Expected output: true |
| 135 | +); |
| 136 | + |
| 137 | +# ------------------------------------------------------------------------------------------------------------ |
| 138 | +# MAIN BODY OF PROGRAM: |
| 139 | +$"=', '; |
| 140 | +for my $aref (@arrays) { |
| 141 | + say ''; |
| 142 | + my $aref1 = $aref->[0]; |
| 143 | + my $aref2 = $aref->[1]; |
| 144 | + say 'Source arrays: ', join(', ', map {'['."@$_".']'} @$aref1); |
| 145 | + say "Target array: (@$aref2)"; |
| 146 | + my $cb = can_build($aref1, $aref2); |
| 147 | + say "Can build? $cb"; |
| 148 | +} |
0 commit comments