aboutsummaryrefslogtreecommitdiff
path: root/challenge-211
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-211')
-rw-r--r--challenge-211/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-211/laurent-rosenfeld/perl/ch-1.pl24
-rw-r--r--challenge-211/laurent-rosenfeld/perl/ch-2.pl47
-rw-r--r--challenge-211/laurent-rosenfeld/raku/ch-1.raku18
-rw-r--r--challenge-211/laurent-rosenfeld/raku/ch-2.raku35
-rw-r--r--challenge-211/robert-dicicco/perl/ch-1.pl87
-rw-r--r--challenge-211/robert-dicicco/perl/ch-2.pl70
-rw-r--r--challenge-211/robert-dicicco/python/ch-1.py79
8 files changed, 361 insertions, 0 deletions
diff --git a/challenge-211/laurent-rosenfeld/blog.txt b/challenge-211/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..e6a617dc8e
--- /dev/null
+++ b/challenge-211/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2023/04/perl-weekly-challenge-211-toeplitz-matrix-and-split-same-average.html
diff --git a/challenge-211/laurent-rosenfeld/perl/ch-1.pl b/challenge-211/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..95d8293879
--- /dev/null
+++ b/challenge-211/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use feature "say";
+
+sub is_toeplitz {
+ my @in = @_;
+ my $j_max = scalar @{$in[0]} - 1;
+ for my $i (1..$#in) {
+ for my $j (1..$j_max) {
+ # say "$i $j $in[$i][$j] $in[$i-1][$j-1]";
+ return "false" if $in[$i][$j] != $in[$i-1][$j-1];
+ }
+ }
+ return "true";
+}
+
+for my $test
+ ( [ [<4 3 2 1>], [<5 4 3 2>], [<6 5 4 3>] ],
+ [ [<3 2 1 0>], [<4 3 2 1>], [<5 4 3 2>] ],
+ [ [<3 2 1 0>], [<4 3 2 1>], [<5 5 3 2>] ],
+ [ [<1 2 3>], [<3 2 1>] ] ) {
+ say "[ ", (join ", ", map "[@$_]", @$test), " ]";
+ say is_toeplitz(@$test), "\n";
+}
diff --git a/challenge-211/laurent-rosenfeld/perl/ch-2.pl b/challenge-211/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..cdba7c0456
--- /dev/null
+++ b/challenge-211/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+use feature "say";
+
+my ($target, @result);
+
+sub avg {
+ my $nb_elems = scalar @_;
+ my $sum = shift;
+ $sum += $_ for @_;
+ return $sum / $nb_elems;
+}
+
+sub find_partition {
+ my @current = @{$_[0]};
+ my @left = @{$_[1]};
+ return if scalar @left <= 1;
+ if (scalar @current > 0 and $target == avg(@current)) {
+ push @result, @current;
+ return;
+ }
+ for my $i (0..$#left) {
+ find_partition( [@current, $left[$i]], [@left[0..$i-1, $i+1..$#left]]);
+ return if @result > 0;
+ }
+}
+
+sub start_partition {
+ my @in = @_;
+ $target = avg @in;
+ @result = ();
+ my @current;
+ find_partition [@current], [@in];
+ return @result;
+}
+
+for my $test ([<1 2 3 4 5 6 7 8>], [<1 2 3>], [<1 3>]) {
+ my @output = start_partition @$test;
+ print "@$test => ";
+ if (scalar @output == 0) {
+ say "false";
+ } else {
+ print "true : [@output] ";
+ my %out = map { $_ => 1 } @output;
+ say "[", join " ", grep { not exists $out{$_} } @$test, "]";
+ }
+}
diff --git a/challenge-211/laurent-rosenfeld/raku/ch-1.raku b/challenge-211/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..4ccc4b99c0
--- /dev/null
+++ b/challenge-211/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,18 @@
+sub is-toeplitz (@in) {
+ for 1..@in.end -> $i {
+ for 1..@in[0].end -> $j {
+ # say "$i $j @in[$i][$j] @in[$i-1][$j-1]";
+ return False if @in[$i][$j] != @in[$i-1][$j-1];
+ }
+ }
+ return True;
+}
+
+
+for ( <4 3 2 1>, <5 4 3 2>, <6 5 4 3> ),
+ ( <3 2 1 0>, <4 3 2 1>, <5 4 3 2> ),
+ ( <3 2 1 0>, <4 3 2 1>, <5 5 3 2> ),
+ ( <1 2 3>, <3 2 1> ) -> @test {
+ say @test;
+ say is-toeplitz(@test), "\n";
+}
diff --git a/challenge-211/laurent-rosenfeld/raku/ch-2.raku b/challenge-211/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..4a085341eb
--- /dev/null
+++ b/challenge-211/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,35 @@
+sub avg (@a) { return ([+] @a) / @a.elems; }
+
+sub find-partition (@current, @left) {
+ return if @left.elems <= 1;
+ # say "Current: ", avg @current if @current.elems > 0;
+ if @current.elems > 0 and $*target == avg @current {
+ push @*result, @current;
+ return;
+ }
+ for 0..@left.end -> $i {
+ find-partition( (@current, @left[$i]).flat,
+ (@left[0..$i-1, $i+1..@left.end]).flat);
+ return if @*result.elems > 0;
+ }
+}
+
+sub start-partition (@in) {
+ my $*target = avg @in;
+ my @*result;
+ my @current;
+ find-partition @current, @in;
+ return @*result;
+}
+
+for <1 2 3 4 5 6 7 8>, <1 2 3>, <1 3> -> @test {
+ my @output = start-partition @test;
+ print @test, " => ";
+ if @output.elems == 0 {
+ say "false";
+ } else {
+ print "true : ";
+ push @output, (@test (-) @output[0]).keys;
+ say @output;
+ }
+}
diff --git a/challenge-211/robert-dicicco/perl/ch-1.pl b/challenge-211/robert-dicicco/perl/ch-1.pl
new file mode 100644
index 0000000000..828a7b2f61
--- /dev/null
+++ b/challenge-211/robert-dicicco/perl/ch-1.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/env perl
+=begin pod
+-------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-04-08
+Challenge 211 Toeplitz Matrix ( Perl )
+-------------------------------------------
+=cut
+use strict;
+use warnings;
+use feature 'say';
+use List::MoreUtils qw(uniq);
+
+#my @matrix = ([4, 3, 2, 1],[5, 4, 3, 2],[6, 5, 4, 3]);
+my @matrix = ([1,2,3],[3,2,1]);
+
+my $cols = scalar @{$matrix[0]};
+my $rows = scalar @matrix;
+
+my $r = 0;
+my $c = 0;
+my @out = ();
+my $flag = 0;
+
+sub CheckDiag {
+ my @o = @{$_[0]};
+ my @o_u = uniq @o;
+ if (scalar @o_u != 1) {
+ $flag = -1
+ }
+}
+
+sub Diag {
+ @out = ();
+ while ($r < $rows) {
+ push(@out,$matrix[$r][$c]);
+ if ($r == $rows - 1) {
+ CheckDiag(\@out);
+ return;
+ } else {
+ if ($c == $cols - 1) {
+ CheckDiag(\@out);
+ last;
+ };
+ $c++;
+ $r++;
+ }
+ }
+}
+
+my $x = 0;
+print "Input matrix = [ ";
+while ($x < $rows) {
+ print "[",@{$matrix[$x++]},"] ";
+}
+
+$c = 0;
+$r = 0;
+while ($c < $cols - 1) {
+ Diag($c,$r);
+ $c++;
+}
+
+$r = 1;
+$c = 0;
+while ($r < $rows - 1) {
+ Diag($c,$r);
+ $r++;
+}
+print "]\n";
+
+$flag == -1 ? say "Output: false" : say "Output: true";
+
+=begin pod
+-------------------------------------------
+SAMPLE OUTPUT
+perl .\Toeplitz.pl
+Input matrix = [ [4321] [5432] [6543] ]
+Output: true
+
+perl .\Toeplitz.pl
+Input matrix = [ [123] [321] ]
+Output: false
+-------------------------------------------
+=cut
+
+
diff --git a/challenge-211/robert-dicicco/perl/ch-2.pl b/challenge-211/robert-dicicco/perl/ch-2.pl
new file mode 100644
index 0000000000..2a8d63a014
--- /dev/null
+++ b/challenge-211/robert-dicicco/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+=begin pod
+----------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-04-09
+Challenge 211 Split Same Average ( Perl )
+----------------------------------------
+=cut
+
+use strict;
+use warnings;
+use feature 'say';
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw/sum/;
+
+my @nums = (1,2,3,4,5,6,7,8);
+#my @nums = (1,3);
+
+my $flag = 0;
+my %seen; # lookup table
+my @sec; # answer
+
+say "Input: \@nums = (@nums)";
+
+if (scalar @nums <= 2) {
+ say "Output: false";
+ exit;
+}
+
+my $iter = combinations(\@nums, 4);
+while (my $pn = $iter->next) {
+ @sec = ();
+ %seen = ();
+ my $first = sum(@$pn) / scalar(@$pn);
+
+ foreach my $item (@$pn) { $seen{$item} = 1 }
+
+ foreach my $item (@nums) {
+ push(@sec, $item) unless exists $seen{$item};
+ }
+
+ my $second = sum(@sec) / scalar @sec;
+ if ($first == $second) { $flag = 1};
+ say "-----> (@$pn),(@sec), $first : $second" if $first == $second;
+}
+
+$flag == 1 ? (say "Output: true") : (say "Output: false");
+
+=begin pod
+----------------------------------------
+SAMPLE OUTPUT
+perl .\SplitSame.pl|more
+Input: @nums = (1 2 3 4 5 6 7 8)
+-----> (1 2 7 8),(3 4 5 6), 4.5 : 4.5
+-----> (1 3 6 8),(2 4 5 7), 4.5 : 4.5
+-----> (1 4 5 8),(2 3 6 7), 4.5 : 4.5
+-----> (1 4 6 7),(2 3 5 8), 4.5 : 4.5
+-----> (2 3 5 8),(1 4 6 7), 4.5 : 4.5
+-----> (2 3 6 7),(1 4 5 8), 4.5 : 4.5
+-----> (2 4 5 7),(1 3 6 8), 4.5 : 4.5
+-----> (3 4 5 6),(1 2 7 8), 4.5 : 4.5
+Output: true
+ perl .\SplitSame.pl
+Input: @nums = (1 3)
+Output: false
+----------------------------------------
+=cut
+
+
+
diff --git a/challenge-211/robert-dicicco/python/ch-1.py b/challenge-211/robert-dicicco/python/ch-1.py
new file mode 100644
index 0000000000..6c17e9d14d
--- /dev/null
+++ b/challenge-211/robert-dicicco/python/ch-1.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+'''
+-------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-04-09
+Challenge 211 Toeplitz Matrix ( Python )
+-------------------------------------------
+'''
+
+matrx = [ [4, 3, 2, 1],[5, 4, 3, 2],[6, 5, 4, 3], ]
+#matrx = [ [1,2,3],[3,2,1] ]
+
+rows = len(matrx)
+cols = len(matrx[0])
+
+r = 0
+c = 0
+
+outarr = []
+flag = 0
+
+def unique(list1):
+ unique_list = []
+ for x in list1:
+ if x not in unique_list:
+ unique_list.append(x)
+ return unique_list
+
+def CheckDiag(o):
+ global flag
+ arr_u = unique(o)
+ ln = len(arr_u)
+ if (ln != 1) :
+ flag = -1
+
+def Diag(c,r):
+ outarr = []
+ while r < rows:
+ outarr.append(matrx[r][c])
+ if r == rows - 1:
+ CheckDiag(outarr)
+ return
+ else:
+ if c == cols - 1:
+ CheckDiag(outarr)
+ break
+ c += 1
+ r += 1
+
+print("Input: @matrix = ",matrx)
+
+c = 0
+r = 0
+
+while c < cols - 1:
+ Diag(c,r)
+ c += 1
+r = 1
+c = 0
+while r < rows - 1:
+ Diag(c,r)
+ r = r + 1
+
+print("Output: false") if (flag == -1) else print("Output: true")
+
+'''
+-------------------------------------------
+SAMPLE OUTPUT
+python .\Toeplitz.py
+Input: @matrix = [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]]
+Output: true
+
+python .\Toeplitz.py
+Input: @matrix = [[1, 2, 3], [3, 2, 1]]
+Output: false
+-------------------------------------------
+'''
+
+