aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-09 11:32:01 +0100
committerGitHub <noreply@github.com>2023-04-09 11:32:01 +0100
commitcc4a2fdd2cd8d6de299dc2ef77d9c372f6b3d448 (patch)
tree8b38625ba4ad68b0e378c48c05131c7c97d10946
parent5afedd00d04d766575fd615e7ba262cbe7ad5143 (diff)
parent17a514fbf0fb17f3391929681c9f7a073618ad8d (diff)
downloadperlweeklychallenge-club-cc4a2fdd2cd8d6de299dc2ef77d9c372f6b3d448.tar.gz
perlweeklychallenge-club-cc4a2fdd2cd8d6de299dc2ef77d9c372f6b3d448.tar.bz2
perlweeklychallenge-club-cc4a2fdd2cd8d6de299dc2ef77d9c372f6b3d448.zip
Merge pull request #7866 from pip/branch-for-challenge-211
Pip Stuart's submission for challenge-211.
-rw-r--r--challenge-211/pip/perl/ch-1.pl38
-rw-r--r--challenge-211/pip/perl/ch-2.pl32
-rw-r--r--challenge-211/pip/raku/ch-1.raku39
-rw-r--r--challenge-211/pip/raku/ch-2.raku33
4 files changed, 142 insertions, 0 deletions
diff --git a/challenge-211/pip/perl/ch-1.pl b/challenge-211/pip/perl/ch-1.pl
new file mode 100644
index 0000000000..d703cb093a
--- /dev/null
+++ b/challenge-211/pip/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #211 - Pip Stuart
+# Task1: Toeplitz Matrix: Submitted by: Mohammad S Anwar; You are given a matrix m x n. Write a script to find out if the given matrix is Toeplitz Matrix.
+# A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
+# Example1:
+# In-put: @matrix = [ [4, 3, 2, 1],
+# [5, 4, 3, 2],
+# [6, 5, 4, 3], ]
+# Output: true
+# Example2:
+# In-put: @matrix = [ [1, 2, 3],
+# [3, 2, 1], ]
+# Output: false
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N48M8DlW';
+sub TpzM {my $mtrx = shift(@_);my $tplz = 1; # below eval string that looks like it's exclusively a 2D matrix array-ref of just comma-separated integerz
+ if (ref($mtrx) ne 'ARRAY' && $mtrx =~ /^\s*\[\s*(\[\s*(\d+\s*,?\s*)+\]\s*,?\s*)+\]\s*$/ ) { $mtrx = eval($mtrx); }
+ my $enen = @{$mtrx}; my $emem = @{$mtrx->[0]} ;
+ for (1 .. $enen - 2) { my $edge = $mtrx->[$_][0];my $roww = $_;my $colm = 0; if ($tplz) { # loop left-side diagonals but not bottom corner
+ while ($roww < $enen && $colm < $emem ) {#say "edge:$edge $mtrx->[$roww][$colm]:mtrx->roww,colm;";
+ if ($edge != $mtrx->[ $roww++][ $colm++]) { $tplz = 0;last; } } } }
+ for (0 .. $emem - 2) { my $edge = $mtrx->[0][$_];my $roww = 0;my $colm = $_; if ($tplz) { # loop top-side diagonals but not right corner
+ while ($roww < $enen && $colm < $emem ) {#say "edge:$edge $mtrx->[$roww][$colm]:mtrx->roww,colm;";
+ if ($edge != $mtrx->[ $roww++][ $colm++]) { $tplz = 0;last; } } } }
+ print '[ ';my $fnif = 1; # set FirstNoIndentFlag;
+ for (@{$mtrx}) { if ($fnif) { $fnif = 0; } else { print "\n "; }
+ printf("[%-10s],", join(', ',@{$_}) ); }
+ say " ] => $tplz;";
+ return( $tplz );
+}
+if (@ARGV) {
+ TpzM(@ARGV);
+} else {
+ TpzM([ [4, 3, 2, 1],
+ [5, 4, 3, 2],
+ [6, 5, 4, 3], ]); # => 1;
+ TpzM([ [1, 2, 3 ],
+ [3, 2, 1 ], ]); # => 0;
+}
diff --git a/challenge-211/pip/perl/ch-2.pl b/challenge-211/pip/perl/ch-2.pl
new file mode 100644
index 0000000000..b757eff298
--- /dev/null
+++ b/challenge-211/pip/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #211 - Pip Stuart
+# Task2: Split Same Average: Submitted by: Mohammad S Anwar; 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.
+# Example1:
+# In-put: @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.
+# Example2:
+# In-put: @nums = (1, 3)
+# Output: false
+# Last date to submit the solution 23:59 (UK Time) Sunday 9th April 2023.
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N48M8KR8';
+sub SSAv { my @numz = @_;my $smav = 0;my $nsiz = @numz;
+ my $spld = 2 ** $nsiz;
+ while (--$spld) {
+ my $splb = sprintf("%0${nsiz}b", $spld);my @bdgz = split(//, $splb);my @left = ();my @rite = ();my $lsum = 0;my $rsum = 0;
+ for (0 .. @bdgz - 1) {
+ if ($bdgz[$_]) {
+ push(@rite, $numz[$_]);$rsum += $numz[$_]; }
+ else {
+ push(@left, $numz[$_]);$lsum += $numz[$_]; } }
+ if (@left && @rite && ($lsum / @left) == ($rsum / @rite)) {#say "splb:$splb avrg:" . ($lsum / @left) . " left:@left rite:@rite;";
+ $smav = 1;last; } } # removing last here && uncommenting say above will show all 16 ways it works for average 4.5 for Example1 of 1..8
+ printf("(%-22s) => $smav;\n", join(', ', @numz));
+ return( $smav);
+}
+if (@ARGV) {
+ SSAv(@ARGV);
+} else {
+ SSAv(1, 2, 3, 4, 5, 6, 7, 8); # => 1;
+ SSAv(1, 3 ); # => 0;
+}
diff --git a/challenge-211/pip/raku/ch-1.raku b/challenge-211/pip/raku/ch-1.raku
new file mode 100644
index 0000000000..c6a729f026
--- /dev/null
+++ b/challenge-211/pip/raku/ch-1.raku
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #211 - Pip Stuart
+# Task1: Toeplitz Matrix: Submitted by: Mohammad S Anwar; You are given a matrix m x n. Write a script to find out if the given matrix is Toeplitz Matrix.
+# A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
+# Example1:
+# In-put: @matrix = [ [4, 3, 2, 1],
+# [5, 4, 3, 2],
+# [6, 5, 4, 3], ]
+# Output: true
+# Example2:
+# In-put: @matrix = [ [1, 2, 3],
+# [3, 2, 1], ]
+# Output: false
+use v6;use MONKEY-SEE-NO-EVAL;my $d8VS='N48MAntE';
+sub TpzM {my $mtrx = [];my $tplz = 1;
+ push(@($mtrx), shift(@_)) while (@_); # below EVAL Str (ing) that looks like it's exclusively characters of a 2D array-ref matrix, commas, && integerz
+ if ($mtrx[0].WHAT.^name eq 'Str' && $mtrx ~~ /^ <[\[\d\,\]\s]>+ $/) { $mtrx = EVAL("$mtrx"); }
+ my $enen = @($mtrx).elems;my $emem = @($mtrx[0]).elems;
+ for (1 .. $enen - 2) { my $edge = $mtrx[$_][0]; my $roww = $_;my $colm = 0; if ($tplz) { # loop left-side diagonals but not bottom corner
+ while ($roww < $enen && $colm < $emem ) { #say "edge:$edge $mtrx[$roww][$colm]:mtrx[roww][colm];";
+ if ($edge != $mtrx[ $roww++][ $colm++]) { $tplz = 0;last; } } } }
+ for (0 .. $emem - 2) { my $edge = $mtrx[0][$_]; my $roww = 0;my $colm = $_; if ($tplz) { # loop top-side diagonals but not right corner
+ while ($roww < $enen && $colm < $emem ) { #say "edge:$edge $mtrx[$roww][$colm]:mtrx[roww][colm];";
+ if ($edge != $mtrx[ $roww++][ $colm++]) { $tplz = 0;last; } } } }
+ print '[ ';my $fnif = 1; # set FirstNoIndentFlag;
+ for (@($mtrx)) { if ($fnif) { $fnif = 0; } else { print "\n "; }
+ printf("[%-10s],", join(', ',@($_)) ); }
+ say " ] => $tplz;";
+ return( $tplz );
+}
+if (@*ARGS) {
+ TpzM(@*ARGS);
+} else {
+ TpzM([ [4, 3, 2, 1],
+ [5, 4, 3, 2],
+ [6, 5, 4, 3], ]); # => 1;
+ TpzM([ [1, 2, 3 ],
+ [3, 2, 1 ], ]); # => 0;
+}
diff --git a/challenge-211/pip/raku/ch-2.raku b/challenge-211/pip/raku/ch-2.raku
new file mode 100644
index 0000000000..4ec5f8cd95
--- /dev/null
+++ b/challenge-211/pip/raku/ch-2.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #211 - Pip Stuart
+# Task2: Split Same Average: Submitted by: Mohammad S Anwar; 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.
+# Example1:
+# In-put: @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.
+# Example2:
+# In-put: @nums = (1, 3)
+# Output: false
+# Last date to submit the solution 23:59 (UK Time) Sunday 9th April 2023.
+use v6;my $d8VS='N48MAnEt';
+sub SSAv { my @numz = @_;my $smav = 0;my $nsiz = @numz.elems;
+ my $spld = 2 ** $nsiz;
+ while (--$spld) {
+ my $splb = sprintf("%0" ~ $nsiz ~ "b", $spld);my @bdgz = split('', $splb, :skip-empty);my @left = ();my @rite = ();my $lsum = 0;my $rsum = 0;
+ for (0 .. @bdgz - 1 ) {
+ if (@bdgz[$_] eq '1') {
+ push(@rite, @numz[$_]);$rsum += @numz[$_]; }
+ else {
+ push(@left, @numz[$_]);$lsum += @numz[$_]; } }
+ if (@left.elems && @rite.elems && ($lsum / @left.elems) == ($rsum / @rite.elems)) {
+# say "splb:$splb avrg:" ~ ($lsum / @left.elems) ~ " left:@left[] rite:@rite[];";
+ $smav = 1;last; } } # removing last here && uncommenting say above will show all 16 ways it works for average 4.5 for Example1 of 1..8
+ printf("(%-22s) => $smav;\n", join(', ', @numz));
+ return( $smav);
+}
+if (@*ARGS) {
+ SSAv(@*ARGS);
+} else {
+ SSAv(1, 2, 3, 4, 5, 6, 7, 8); # => 1;
+ SSAv(1, 3 ); # => 0;
+}