aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-211/solathian/perl/ch-1.pl52
-rw-r--r--challenge-211/solathian/perl/ch-2.pl97
2 files changed, 149 insertions, 0 deletions
diff --git a/challenge-211/solathian/perl/ch-1.pl b/challenge-211/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..987c547731
--- /dev/null
+++ b/challenge-211/solathian/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!usr/bin/perl
+use v5.36;
+
+use builtin qw(indexed true false);
+no warnings 'experimental';
+
+# Challenge 211- 1 - Toeplitz Matrix
+# You are given a matrix m x n.
+
+# Write a script to find out if the given matrix is Toeplitz Matrix.
+
+
+my @matrix = ( [4, 3, 2, 1],
+ [5, 4, 3, 2],
+ [6, 5, 4, 3]);
+
+Toeplitz(\@matrix); # Output: true
+
+my @matrix2 = ( [1, 2, 3],
+ [3, 2, 1]);
+
+Toeplitz(\@matrix2); # Output: true
+
+
+
+sub Toeplitz($matrixRef)
+{
+ my $retVal = true;
+
+ OUTER:
+ foreach my ($i, $row) (indexed @$matrixRef)
+ {
+ foreach my ($j, $jVal) (indexed @$row)
+ {
+ # skip the elements in the last column and the row, since we cannot check diagonally
+ next if($i == $#$matrixRef);
+ next if($j == $#$row);
+
+ if($matrixRef->[$i][$j] != $matrixRef->[$i + 1][$j + 1] )
+ {
+ $retVal = false;
+ last OUTER;
+ }
+
+ }
+ }
+
+ if($retVal){ say "true" }
+ else{ say "false" }
+
+
+} \ No newline at end of file
diff --git a/challenge-211/solathian/perl/ch-2.pl b/challenge-211/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..27f2c07669
--- /dev/null
+++ b/challenge-211/solathian/perl/ch-2.pl
@@ -0,0 +1,97 @@
+#!usr/bin/perl
+use v5.36;
+
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(sum);
+use builtin qw( true false);
+no warnings 'experimental';
+
+# Challenge 211 - 2 - Split Same Average
+# You are given an array of integers.
+# Write a script to find out if the given can be split into two separate arrays whose average are the same.
+# (1, 2, 3, 4, 5, 6, 7, 8)
+
+
+
+ssa(1, 2, 3, 4, 5, 6, 7, 8);
+# ssa(1, 2, 3, 4, 4, 6, 7, 8);
+# We can split the given array into (1, 4, 5, 8) and (2, 3, 6, 7).
+# The average of the two arrays are the same i.e. 4.5.
+# Output: true
+
+ssa(1, 3);
+
+# Output: false
+
+sub getLeftOut($originalArray, $firstArray)
+{
+ my @secondArray;
+
+ foreach my $elem (@$originalArray)
+ {
+ next if($elem ~~ @$firstArray);
+
+ push(@secondArray, $elem);
+
+ # yes, currently it goes haywire if the array is not unique
+ }
+
+ return \@secondArray;
+}
+
+sub splitArray($arrayRef, $firstSize)
+{
+ my @resultArray;
+
+ foreach my $firstPart ( combinations($arrayRef, $firstSize))
+ {
+ my $secondArray = getLeftOut($arrayRef, $firstPart);
+
+ push(@resultArray, [$firstPart, $secondArray])
+ }
+
+ return \@resultArray;
+}
+
+sub avg($list)
+{
+ return sum(@$list) / @$list;
+}
+
+
+
+sub ssa(@list)
+{
+
+ say "Array: @list";
+
+ my $currentSize = 1;
+ my $returnFlag = false;
+
+ OUTER:
+ while($currentSize < (@list - 1))
+ {
+ my $splitArrays = splitArray(\@list, $currentSize);
+
+ foreach my $splitArray (@$splitArrays)
+ {
+ my $first = $splitArray->[0];
+ my $second = $splitArray->[1];
+
+ if(avg($first) == avg($second))
+ {
+
+ say "Found an array:";
+ say "Average is:" . avg($first);
+ say '(' . join(',', @$first) . '),('. join(',', @$second) . ')';
+ say "";
+ $returnFlag = true;
+ last OUTER;
+ }
+ }
+
+ $currentSize++;
+ }
+
+ if(!$returnFlag){say "Did not found such array"}
+} \ No newline at end of file