aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2023-01-22 19:45:35 -0500
committerAdam Russell <ac.russell@live.com>2023-01-22 19:45:35 -0500
commit7484c35eafb305da031e35a221cd55582baa2d27 (patch)
tree0ef2feed4b0915fe98ce8d75e7c97ea6ce3baba8
parent952f98a3d4e479992cd18e544ebb441a952f7159 (diff)
downloadperlweeklychallenge-club-7484c35eafb305da031e35a221cd55582baa2d27.tar.gz
perlweeklychallenge-club-7484c35eafb305da031e35a221cd55582baa2d27.tar.bz2
perlweeklychallenge-club-7484c35eafb305da031e35a221cd55582baa2d27.zip
initial commit
-rw-r--r--challenge-200/adam-russell/perl/ch-1.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-200/adam-russell/perl/ch-1.pl b/challenge-200/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..19784d8447
--- /dev/null
+++ b/challenge-200/adam-russell/perl/ch-1.pl
@@ -0,0 +1,40 @@
+use v5.36;
+##
+# You are given an array of integers.
+# Write a script to find out all Arithmetic Slices for the given array of integers.
+##
+use boolean;
+
+sub is_arithmetic{
+ my($slice) = @_;
+ do{
+ return false if $slice->[$_] != $slice->[$_ + 1] - 1;
+ } for 0 .. @{$slice} - 2;
+ return true;
+}
+
+sub arithmetic_slices{
+ my @slices;
+ my @a = @_;
+ return ([]) if @a < 3;
+ do{
+ my $i = $_;
+ do{
+ my $slice = [@a[$i .. $i + $_]] if $a[$i] && $a[$i + $_];
+ push @slices, $slice if $slice && is_arithmetic($slice);
+ } for 2 .. @a - 1;
+ } for 0 .. @a - 3;
+ return @slices;
+}
+
+MAIN:{
+ say q/1, 2, 3, 4/;
+ for my $slice (arithmetic_slices(1, 2, 3, 4)){
+ say q/[/ . join(q/, /, @{$slice}) . q/]/;
+ }
+ say q//;
+ say q/2/;
+ for my $slice (arithmetic_slices(2)){
+ say q/[/ . join(q/, /, @{$slice}) . q/]/;
+ }
+} \ No newline at end of file