aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2023-04-06 23:59:18 +0200
committerpme <hauptadler@gmail.com>2023-04-06 23:59:18 +0200
commitee2a7ba78fb200737d357e7b72120a325ecfdd37 (patch)
treeac533e41521a23fee08aa4a572692ad59683695a
parent8995f1e0c60ae53e54c0e4cd05dfa27cd3f1a841 (diff)
downloadperlweeklychallenge-club-ee2a7ba78fb200737d357e7b72120a325ecfdd37.tar.gz
perlweeklychallenge-club-ee2a7ba78fb200737d357e7b72120a325ecfdd37.tar.bz2
perlweeklychallenge-club-ee2a7ba78fb200737d357e7b72120a325ecfdd37.zip
Solutions for challange 211
-rwxr-xr-xchallenge-211/peter-meszaros/perl/ch-1.pl51
-rwxr-xr-xchallenge-211/peter-meszaros/perl/ch-2.pl81
2 files changed, 132 insertions, 0 deletions
diff --git a/challenge-211/peter-meszaros/perl/ch-1.pl b/challenge-211/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..41ac51a6c5
--- /dev/null
+++ b/challenge-211/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr//bin/perl
+# Example 1
+#
+# Input: @matrix = [ [4, 3, 2, 1],
+# [5, 4, 3, 2],
+# [6, 5, 4, 3],
+# ]
+# Output: true
+#
+# Example 2
+#
+# Input: @matrix = [ [1, 2, 3],
+# [3, 2, 1],
+# ]
+# Output: false
+
+use strict;
+use warnings;
+use Test::More;
+
+my $cases = [
+ [[4, 3, 2, 1],
+ [5, 4, 3, 2],
+ [6, 5, 4, 3],
+ ],
+ [[1, 2, 3],
+ [3, 2, 1],
+ ],
+];
+
+sub is_toeplitz
+{
+ my $mref = shift;
+
+ my $m = $mref->@* - 1;
+ my $n = $mref->[0]->@* - 1;
+ for(my $i=0; $i < $m; ++$i) {
+ for(my $j=0; $j < $n; ++$j) {
+ return 0 if ($mref->[$i]->[$j] != $mref->[$i + 1][$j + 1]);
+ }
+ }
+
+ return 1;
+}
+
+is(is_toeplitz($cases->[0]), 1, 'Toeplitz');
+is(is_toeplitz($cases->[1]), 0, 'Non-Toeplitz');
+done_testing();
+
+exit 0;
+
diff --git a/challenge-211/peter-meszaros/perl/ch-2.pl b/challenge-211/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..470ed3ca42
--- /dev/null
+++ b/challenge-211/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr//bin/perl
+# Example 1:
+#
+# Input: @nums = (1, 2, 3, 4, 5, 6, 7, 8)
+# Output: true
+#
+# 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.
+#
+# Example 2:
+#
+# Input: @list = (1, 3)
+# Output: false
+
+use strict;
+use warnings;
+use List::Util qw/sum0/;
+use Test::More;
+
+my $cases = [
+ [1, 2, 3, 4, 5, 6, 7, 8],
+ [1, 3],
+];
+
+sub avg
+{
+ my $a = shift;
+ return sum0(@$a) / @$a;
+}
+
+sub combine
+{
+ my ($list, $n) = @_;
+ die "Insufficient list members" if $n > @$list;
+
+ return map [$_], @$list if $n <= 1;
+
+ my @comb;
+
+ for (my $i = 0; $i+$n <= @$list; ++$i) {
+ my $val = $list->[$i];
+ my @rest = @$list[$i+1..$#$list];
+ push @comb, [$val, @$_] for combine(\@rest, $n-1);
+ }
+
+ return @comb;
+}
+
+sub split_array
+{
+ my $lref = shift;
+
+ my $idx = [0..$#$lref];
+ my @idxlist;
+ my $n = 0;
+ while ($n < int(@$idx / 2)) {
+ push @idxlist, combine($idx, ++$n);
+ }
+
+ for my $i (@idxlist) {
+ my @l = $lref->@[@$i];
+ my @r = @$lref;
+ delete @r[@$i];
+ @r = grep { defined } @r;
+ my $lavg = avg(\@l);
+ my $ravg = avg(\@r);
+ if (abs($lavg - $ravg) < 0.001) {
+ #print join('|', @$i), '--', join('|', @l), ' - ', join('|', @r),
+ # ' -> ', $lavg, '<=>', $ravg, "\n";
+ return 1
+ }
+ }
+ return 0;
+}
+
+is(split_array($cases->[0]), 1, '[1, 2, 3, 4, 5, 6, 7, 8]');
+is(split_array($cases->[1]), 0, '[1, 3]');
+done_testing();
+
+exit 0;
+