aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolathian <horvath6@gmail.com>2023-01-22 20:51:05 +0100
committerSolathian <horvath6@gmail.com>2023-01-22 20:51:05 +0100
commit9239115a12404f25885914b0e962402c3cabd268 (patch)
treea1d52a1428acfbd4dc0baae5dd8372b394965ed9
parent952f98a3d4e479992cd18e544ebb441a952f7159 (diff)
downloadperlweeklychallenge-club-9239115a12404f25885914b0e962402c3cabd268.tar.gz
perlweeklychallenge-club-9239115a12404f25885914b0e962402c3cabd268.tar.bz2
perlweeklychallenge-club-9239115a12404f25885914b0e962402c3cabd268.zip
Adding files
-rw-r--r--challenge-200/solathian/perl/ch-1.pl82
1 files changed, 82 insertions, 0 deletions
diff --git a/challenge-200/solathian/perl/ch-1.pl b/challenge-200/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..570fe54d23
--- /dev/null
+++ b/challenge-200/solathian/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!usr/bin/perl
+use v5.36;
+
+use builtin 'indexed';
+no warnings builtin::indexed;
+
+# Challange 200 - 1 - Arithmetic Slices
+# You are given an array of integers.
+
+# Write a script to find out all Arithmetic Slices for the given array of integers.
+
+# An integer array is called arithmetic if it has at least 3 elements and
+# the differences between any three consecutive elements are the same.
+
+use Data::Dumper;
+
+arithmSlice(1,2,3,4); # Output: (1,2,3), (2,3,4), (1,2,3,4)
+arithmSlice(1,2,3,4, 7);
+arithmSlice(2); # Output ()
+# arithmSlice(2,3);
+arithmSlice(1,2,3,4,5);
+# arithmSlice(2, 4, 6, 8);
+# arithmSlice(2);
+
+
+
+sub arithmSlice(@array)
+{
+ my @arraySlices;
+ my @resultArrays;
+
+ if(@array < 3)
+ {
+ say "()";
+ return;
+ }
+
+ # create partitions
+ for(my $chain = 2; $chain < @array; $chain++)
+ {
+ my $i = 0;
+
+ while( ($i + $chain) < @array)
+ {
+ my @slice = @array[$i .. ($i + $chain)];
+ push (@arraySlices, \@slice);
+
+ $i++;
+ }
+
+ }
+
+ # check the slices
+ OUTER: foreach my $arrayRef (@arraySlices)
+ {
+ my $diff = $arrayRef->[1] - $arrayRef->[0];
+ my $last;
+
+ foreach my ($i, $value) (indexed @$arrayRef)
+ {
+ if( not defined $last)
+ {
+ $last = $value;
+ next;
+ }
+
+ if(($value - $last) != $diff)
+ {
+ next OUTER;
+ }
+
+ $last = $value;
+ }
+
+ push(@resultArrays, $arrayRef)
+ }
+
+ # create output
+ map{ print "(", join(",", @$_ ), ")," } @resultArrays;
+ say "";
+
+} \ No newline at end of file