aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoneWolfiNTj <Hatley.Software@gmail.com>2023-01-22 13:05:33 -0800
committerLoneWolfiNTj <Hatley.Software@gmail.com>2023-01-22 13:05:33 -0800
commit0c2c33a327dfca05bf91112b6d78d4c598176855 (patch)
treeee073510607eb3075fe84a7b5c48994f899eef31
parentf1249991c0a493ec1340df80d1e5b4b1dc4db05a (diff)
downloadperlweeklychallenge-club-0c2c33a327dfca05bf91112b6d78d4c598176855.tar.gz
perlweeklychallenge-club-0c2c33a327dfca05bf91112b6d78d4c598176855.tar.bz2
perlweeklychallenge-club-0c2c33a327dfca05bf91112b6d78d4c598176855.zip
Added challenge 200-1.
-rwxr-xr-xchallenge-200/robbie-hatley/perl/ch-1.pl50
1 files changed, 47 insertions, 3 deletions
diff --git a/challenge-200/robbie-hatley/perl/ch-1.pl b/challenge-200/robbie-hatley/perl/ch-1.pl
index 2d69624d72..789427c264 100755
--- a/challenge-200/robbie-hatley/perl/ch-1.pl
+++ b/challenge-200/robbie-hatley/perl/ch-1.pl
@@ -15,15 +15,59 @@ Example 2: Input: @array = (2) Output: () as no slice found.
=cut
+# IO NOTES:
+#
# NOTE: Input is by either built-in array of arrays, or by @ARGV. If using
# @ARGV, input should be a space-separated list of integers, which will
# be interpreted as a single array.
-
+#
# NOTE: Output is to stdout and will be a list of all arithmetic slice for
# each input array.
# PRELIMINARIES:
use v5.36;
+$,=" ";
+
+# SUBROUTINES:
+
+sub is_arith($slice_ref){
+ my @slice = @{$slice_ref};
+ my $size = scalar @slice;
+ if ( $size < 3 ) {return 0;}
+ my $init = $slice[1]-$slice[0];
+ for ( my $idx = 2 ; $idx <= $#slice ; ++$idx ){
+ if ($slice[$idx] - $slice[$idx-1] != $init) {return 0}}
+ return 1;}
+
+sub get_arith_slices($array_ref){
+ my @array = @{$array_ref};
+ my @slices = ();
+ my @arith_slices = ();
+ my $size = scalar @array;
+ my @masks = (0..((2**$size)-1));
+ foreach my $mask (@masks){
+ my @slice = ();
+ for ( my $idx = 0 ; $idx <= $#array ; ++$idx ){
+ my $yesno = ($mask/2**($size-$idx-1))%2;
+ if ($yesno) {push @slice, $array[$idx]}}
+ push @slices, \@slice;}
+ foreach my $slice_ref (@slices){
+ if (is_arith($slice_ref)) {push @arith_slices, $slice_ref}}
+ return @arith_slices;}
+
+# DEFAULT INPUT:
+my @arrays = ([1,2,3,4],[2]);
+
+# NON-DEFAULT INPUT:
+if (@ARGV) {@arrays = ([@ARGV])}
-say "Sorry, this is just a stub; ran out of time.";
-say "\"Task 2\", however, is fully functional.";
+# MAIN BODY OF SCRIPT:
+for (@arrays){
+ my $array_ref = $_;
+ my @array = @{$array_ref};
+ my @arith_slices = get_arith_slices($array_ref);
+ my $num_arith_slices = scalar @arith_slices;
+ say '';
+ say "Array = (@array)";
+ say "Found $num_arith_slices arithmetic slices:";
+ for (@arith_slices) {my @slice=@{$_}; say "(@slice)";}} \ No newline at end of file