Skip to content

Commit 9b8eaba

Browse files
robbie-hatleyrobbie-hatley
authored andcommitted
Robbie Hatley's solutions, in Perl, for The Weekly Challenge manwar#344.
1 parent 9a7dd06 commit 9b8eaba

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://hatley-software.blogspot.com/2025/10/robbie-hatleys-solutions-in-perl-for_26.html
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env perl
2+
3+
=pod
4+
5+
--------------------------------------------------------------------------------------------------------------
6+
TITLE AND ATTRIBUTION:
7+
Solutions in Perl for The Weekly Challenge 344-1,
8+
written by Robbie Hatley on Wed Oct 22, 2025.
9+
10+
--------------------------------------------------------------------------------------------------------------
11+
PROBLEM DESCRIPTION:
12+
Task 344-1: Array Form Compute
13+
Submitted by: Mohammad Sajid Anwar
14+
You are given an array of integers, @ints and an integer, $x.
15+
Write a script to add $x to the integer in the array-form.
16+
The array form of an integer is a digit-by-digit representation
17+
stored as an array, where the most significant digit is at the
18+
0th index.
19+
20+
Example 1
21+
Input: @ints = (1, 2, 3, 4), $x = 12
22+
Output: (1, 2, 4, 6)
23+
24+
Example 2
25+
Input: @ints = (2, 7, 4), $x = 181
26+
Output: (4, 5, 5)
27+
28+
Example 3
29+
Input: @ints = (9, 9, 9), $x = 1
30+
Output: (1, 0, 0, 0)
31+
32+
Example 4
33+
Input: @ints = (1, 0, 0, 0, 0), $x = 9999
34+
Output: (1, 9, 9, 9, 9)
35+
36+
Example 5
37+
Input: @ints = (0), $x = 1000
38+
Output: (1, 0, 0, 0)
39+
40+
--------------------------------------------------------------------------------------------------------------
41+
PROBLEM NOTES:
42+
This is just split(//, join('', @ints)+$x).
43+
44+
--------------------------------------------------------------------------------------------------------------
45+
IO NOTES:
46+
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
47+
single-quoted array of arrays, with each inner array having two elements which are an array of integers and
48+
an integer, in proper Perl syntax, like so:
49+
50+
./ch-1.pl '([[1,6,0,4], 37], [[9,6,0,2], 626])'
51+
52+
Output is to STDOUT and will be each input followed by the corresponding output.
53+
54+
=cut
55+
56+
# ------------------------------------------------------------------------------------------------------------
57+
# PRAGMAS, MODULES, AND SUBS:
58+
59+
use v5.36;
60+
use utf8::all;
61+
62+
# Add $x to array-form integer @ints:
63+
sub sum ($x, @ints) {
64+
split(//,join('',@ints)+$x)}
65+
66+
# ------------------------------------------------------------------------------------------------------------
67+
# INPUTS:
68+
my @arrays = @ARGV ? eval($ARGV[0]) :
69+
(
70+
# Example #1 input:
71+
[[1, 2, 3, 4], 12],
72+
# Expected output: (1, 2, 4, 6)
73+
74+
# Example #2 input:
75+
[[2, 7, 4], 181],
76+
# Expected output: (4, 5, 5)
77+
78+
# Example #3 input:
79+
[[9, 9, 9], 1],
80+
# Expected output: (1, 0, 0, 0)
81+
82+
# Example #4 input:
83+
[[1, 0, 0, 0, 0], 9999],
84+
# Expected output: (1, 9, 9, 9, 9)
85+
86+
# Example #5 input:
87+
[[0], 1000],
88+
# Expected output: (1, 0, 0, 0)
89+
);
90+
91+
# ------------------------------------------------------------------------------------------------------------
92+
# MAIN BODY OF PROGRAM:
93+
$"=', ';
94+
for my $aref (@arrays) {
95+
say '';
96+
my @i = @{$aref->[0]};
97+
my $x = $aref->[1];
98+
say "array = (@i)";
99+
say "integer = $x";
100+
my @s = sum($x, @i);
101+
say "sum = (@s)";
102+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

Comments
 (0)