diff options
| -rw-r--r-- | challenge-344/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-344/robbie-hatley/perl/ch-1.pl | 102 | ||||
| -rwxr-xr-x | challenge-344/robbie-hatley/perl/ch-2.pl | 148 |
3 files changed, 251 insertions, 0 deletions
diff --git a/challenge-344/robbie-hatley/blog.txt b/challenge-344/robbie-hatley/blog.txt new file mode 100644 index 0000000000..7f602aab42 --- /dev/null +++ b/challenge-344/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/10/robbie-hatleys-solutions-in-perl-for_26.html diff --git a/challenge-344/robbie-hatley/perl/ch-1.pl b/challenge-344/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..e2865038ef --- /dev/null +++ b/challenge-344/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,102 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 344-1, +written by Robbie Hatley on Wed Oct 22, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This is just split(//, join('', @ints)+$x). + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays, with each inner array having two elements which are an array of integers and +an integer, in proper Perl syntax, like so: + +./ch-1.pl '([[1,6,0,4], 37], [[9,6,0,2], 626])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Add $x to array-form integer @ints: + sub sum ($x, @ints) { + split(//,join('',@ints)+$x)} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example #1 input: + [[1, 2, 3, 4], 12], + # Expected output: (1, 2, 4, 6) + + # Example #2 input: + [[2, 7, 4], 181], + # Expected output: (4, 5, 5) + + # Example #3 input: + [[9, 9, 9], 1], + # Expected output: (1, 0, 0, 0) + + # Example #4 input: + [[1, 0, 0, 0, 0], 9999], + # Expected output: (1, 9, 9, 9, 9) + + # Example #5 input: + [[0], 1000], + # Expected output: (1, 0, 0, 0) +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + my @i = @{$aref->[0]}; + my $x = $aref->[1]; + say "array = (@i)"; + say "integer = $x"; + my @s = sum($x, @i); + say "sum = (@s)"; +} diff --git a/challenge-344/robbie-hatley/perl/ch-2.pl b/challenge-344/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..fc0b4bb9cd --- /dev/null +++ b/challenge-344/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,148 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 344-2, +written by Robbie Hatley on Sat Oct 25, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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] + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +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 matches, return 'false'. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of arrays of arrays of digits, in proper Perl syntax, like so: + +./ch-2.pl '([[[1,7],[6,3]], [6,1,7,3]], [[[1,7],[6,3]], [6,3,1,7]])' + +Of each inner array, the first element will be the "source" described in the problem description, and the +second element will be the "target". + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Are two arrays equal? + sub is_equal ($aref1, $aref2) { + my $m = scalar @$aref1; + my $n = scalar @$aref2; + if ($m != $n) {return 0} + for (0..$n-1) {if ($$aref1[$_] != $$aref2[$_]) {return 0}} + return 1} + + # Can a target be built from a source? + sub can_build ($aref1, $aref2) { + my @src = @$aref1; # Array of source arrays. + my $ssz = scalar(@src); # Number of source arrays. + my @tar = @$aref2; # Target array. + my $tsz = scalar(@tar); # Target size. + for my $idx (0..$ssz-1) { + my @sa = @{$src[$idx]}; # Source array. + my $sas = scalar(@sa); # Size of source array. + # Skip @sa if it's bigger than @tar: + next if ($sas > $tsz); + # Get the first $sas elements of @tar: + my @prefix = @tar[0..$sas-1]; + # Skip to next source array if @sa doesn't match @prefix: + next if (!is_equal(\@sa,\@prefix)); + # If we get to here, sub-array matches beginning of target. + # If this is a total match, return 'true': + return 'true' if $sas == $tsz; + # If we get to here, it's a partial match. Call this function again, + # recursively, and send it the unused portions of @src and @trg: + my @psrc = (@src[0..$idx-1], @src[$idx+1..$ssz-1]); + my @ptrg = (@tar[$sas..$tsz-1]); + my $result = can_build(\@psrc,\@ptrg); + # If that recursive function call returned 'true', return 'true': + return 'true' if 'true' eq $result} + # If we get to here, no way exists to build @src from @tar, + # so return 'false': + return 'false'} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [[[2,3], [1], [4]], [1, 2, 3, 4]], + # Expected output: true + + # Example 2 input: + [[[1,3], [2,4]], [1, 2, 3, 4]], + # Expected output: false + + # Example 3 input: + [[[9,1], [5,8], [2]], [5, 8, 2, 9, 1]], + # Expected output: true + + # Example 4 input: + [[[1], [3]], [1, 2, 3]], + # Expected output: false + + # Example 5 input: + [[[7,4,6]], [7, 4, 6]], + # Expected output: true +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + my $aref1 = $aref->[0]; + my $aref2 = $aref->[1]; + say 'Source arrays: ', join(', ', map {'['."@$_".']'} @$aref1); + say "Target array: (@$aref2)"; + my $cb = can_build($aref1, $aref2); + say "Can build? $cb"; +} |
