aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-09 11:17:17 +0100
committerGitHub <noreply@github.com>2023-04-09 11:17:17 +0100
commit7999f596deffa1117d403a57fdf8a2832fed9c6e (patch)
tree7bad91fcd69eb7bbcd0476612395ef800b5a7465
parent419cb48e0bd7736f9b625a9f60ce52bc77be8f7a (diff)
parent8662fc692022781222416ddbb447ed59ff559b28 (diff)
downloadperlweeklychallenge-club-7999f596deffa1117d403a57fdf8a2832fed9c6e.tar.gz
perlweeklychallenge-club-7999f596deffa1117d403a57fdf8a2832fed9c6e.tar.bz2
perlweeklychallenge-club-7999f596deffa1117d403a57fdf8a2832fed9c6e.zip
Merge pull request #7858 from 0rir/211
211
-rw-r--r--challenge-211/0rir/raku/ch-1.raku196
-rw-r--r--challenge-211/0rir/raku/ch-2.raku91
2 files changed, 287 insertions, 0 deletions
diff --git a/challenge-211/0rir/raku/ch-1.raku b/challenge-211/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..ef8770fe98
--- /dev/null
+++ b/challenge-211/0rir/raku/ch-1.raku
@@ -0,0 +1,196 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+211-1: Toeplitz Matrix Submitted by: Mohammad S Anwar
+Given a matrix m x n, 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.
+
+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
+=end comment
+
+my @Test =
+ # shorted
+ [ [1,],], True,
+ [ [1,2,3,4,5,],], True,
+ [ [1,],[2,],[3,],[4,],[5],], True,
+
+ # examples
+ [ [4,3,2,1], [5,4,3,2], [6,5,4,3],], True,
+ [ [1,2,3], [3,2,1],], False,
+
+ # more
+ [ [1,1,1,1,], [1,1,1,1,], [1,1,1,1,], [1,1,1,1,],], True,
+ [ [1,2],[2,1],], True,
+ [ [1,2,3,], [4,1,2,], [5,4,1,],], True,
+ [ [0,1,2],[1,0,1],[2,1,0],[3,2,1],[4,3,2],], True,
+
+ [ [9,9,],
+ [9,0,],], False,
+
+ [ [0,9,],
+ [9,9,],], False,
+
+ [ [0,1,9],
+ [1,0,1],
+ [9,1,0],], True,
+
+ [ [9,1,9],
+ [1,0,1],
+ [9,1,0],], False,
+
+ [ [0,9,9],
+ [1,0,1],
+ [9,1,0],], False,
+
+ [ [0,1,9],
+ [9,0,1],
+ [9,1,0],], False,
+
+ [ [0,1,9],
+ [1,9,1],
+ [9,1,0],], False,
+
+ [ [0,1,9],
+ [1,0,9],
+ [9,1,0],], False,
+
+ [ [0,1,9],
+ [1,0,1],
+ [9,9,0],], False,
+
+ [ [0,1,9],
+ [1,0,1],
+ [9,1,9],], False,
+
+ [ [0,2,],
+ [1,0,],
+ [2,1,],
+ [0,2,],], True,
+
+ [ [0,1,5,6,],
+ [1,0,1,5,],
+ [2,1,0,1,],
+ [6,2,1,0,],], True,
+
+ [ [0,1,2,6,],
+ [1,0,1,2,],
+ [2,1,0,1,],
+ [6,2,1,0,],], True,
+
+ [ [0,1,5,6,7,],
+ [1,0,1,5,6,],
+ [2,1,0,1,5,],
+ [0,2,1,0,1,],], True,
+
+ [ [0,1,5,6,7,],
+ [1,0,1,5,6,],
+ [2,1,0,1,5,],
+ [0,2,1,0,1,],], True,
+;
+
+my @Dead =
+ [ ],
+ [ [],],
+ [ [1,],[],],
+ [ [1,],[1,2],],
+ [ [4,3,2,1], [5,4,3,2], [5,4,3],],
+;
+
+plan @Test + @Dead;
+
+
+sub is-toeplitz( @a -->Bool){
+
+ invalid( @a);
+
+ my ($cols, $rows) = @a[0].end, @a.end;
+
+ return True if $cols == 0 or $rows == 0; # very short diagonals
+
+ my $o = [ $rows-1, 0];
+
+ loop {
+ return False unless $o.&diag;
+ $o.&next-diag;
+ last if $o.&last-diag;
+ }
+ return True;
+
+ # --- not reached ----
+
+ constant R = 0;
+ constant C = 1;
+
+ sub val( $d -->Any){ @a[ $d[R]][ $d[C]] }
+
+ sub next-diag( $d is rw -->Array){ # alter dyad to index next diag origin
+ when $d[R] > 0 { --$d[R]; $d }
+ when $d[R] == 0 { ++$d[C]; $d }
+ die 'not reached ( stupid programmer either way)';
+ }
+
+ sub last-diag( $d -->Bool){ $d[R] == 0 and $d[C] == $cols}
+
+ sub diag( $d -->Bool){
+ my $ref = $d.&val;
+ my ($R, $C) = 1 + $d[R], 1 + $d[C];
+ while $R ≤ $rows and $C ≤ $cols {
+ return False if $ref ≠ @a[$R++][$C++];
+ }
+ return True;
+ }
+ sub invalid( @a -->Nil){
+ when @a ~~ Empty { die "Empty" }
+ when @a.any ~~ Empty { die "Empty elem" }
+ with @a[1..^@a].first( {.end !~~ @a[0].end} ) {
+ die "Misshapened"; }
+ return;
+ }
+}
+
+for @Dead -> @in {
+ dies-ok { is-toeplitz( @in )},
+ "Died-ok: Empty, Empty elem, or misshapened";
+}
+for @Test -> @in, $exp {
+ lives-ok { is-toeplitz( @in )},
+ "Lives-ok: Empty, Empty elem, or misshapened";
+ is is-toeplitz(@in), $exp, "$exp <- @in.raku()";
+}
+done-testing;
+
+
+my @matrix = [ [0,1,5,6,7,],
+ [1,0,1,5,6,],
+ [2,1,0,1,5,],
+ [0,2,1,0,1,],];
+
+sub matrix-say( @m is copy ) {
+ for @matrix -> $row is rw {
+ $row = $row.join( ",");
+ }
+ print "\nInput @matrix = [ [";
+ print @matrix.join: "],\n [";
+ print "],\n ]\n";
+}
+
+matrix-say( @matrix);
+say "Output: ", is-toeplitz( @matrix);
+
+exit;
+
diff --git a/challenge-211/0rir/raku/ch-2.raku b/challenge-211/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..3828fa95c6
--- /dev/null
+++ b/challenge-211/0rir/raku/ch-2.raku
@@ -0,0 +1,91 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+211-2: Split Same Average Submitted by: Mohammad S Anwar
+Given an array of integers, find out if the array can be split into two
+separate arrays whose averages are the same.
+
+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
+=end comment
+
+my @Test =
+ # shorts -- atomic
+ [ ], False,
+ [ 1], False,
+ # given examples
+ [ 1,3], False,
+ [ 2,3,6,7, 1,4,5,8], True,
+ # more
+ [ 11 xx 111], True,
+ [ 0,0,0,0,0], True,
+ [ 1,1], True,
+ [ 1,2], False,
+ [ 1,2,4], False,
+ [ 2,2,3], False,
+ [ 1,-1], False,
+ [ 1,14,15], False,
+ [ 1,14, 0,15], True,
+ [ 0,0,1,14,15], False,
+ [ 0,1,14, 0,0,15], True,
+ [ 1,2,3,12,13,14,15], False,
+ [ 0,7, 1,2,3,4,5,6], True,
+ [ 0,1,1,2,3,4,5,6,7], False,
+ [ 1,4, 2,3], True,
+ [ 2, 0,1,3,4], True,
+ [ 1,4,5,7, 0,2,3,6,8], True,
+ [ -2, -1,-1,0], True,
+ [ -3,-2,-1,1], False,
+ [ -3…0], True,
+ [ -3…1], True,
+ [ -1,0,2,6,7, 1,1,3,4,5], True,
+ [ -1,-1,2,6,7, 0,1,3,4,5], True,
+ [ -1,1,2,3,4,5,6,7], False,
+ [ -10…10], True,
+ [ -9…10], True,
+ [ 1…12], True,
+ [ 0…12], True,
+ [ -2…1], True,
+ [ -4…4], True,
+;
+
+# return True if so 'subset.elems × other.sum == subset.sum × other.elems'
+# is found, else False.
+multi same-average-parts2( @in where *.elems ≤ 1 -->Bool ){ False }
+multi same-average-parts2( @in -->Bool){
+
+ my Int $total = [+] @in;
+
+ my $prev;
+ for 1..(@in.elems div 2) -> $a-elems {
+ for @in.combinations( $a-elems).sort -> $a {
+ my $b-val = $a-elems × ($total - [+] $a);
+
+ if $b-val == ([+] $a) × ( @in.elems - $a-elems) {
+ return True;
+ } } }
+ return False;
+}
+
+plan @Test/2;
+
+for @Test -> @t, $exp {
+ is same-average-parts2( @t), $exp, "$exp <- @t[]";
+}
+done-testing;
+
+my $ints = @Test[*-2];
+say "\nInput: \$ints = $ints[]"
+ ,"\nOutput: &same-average-parts2($ints)";
+
+exit;