aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-21 09:08:34 +0000
committerGitHub <noreply@github.com>2024-02-21 09:08:34 +0000
commit1036bfd73a5af401f2fb49d91cb22d62f9a782eb (patch)
tree01c4410d1f39bb8af7d576ed7bc4364d20666ca2
parent78905a4fcda243f40aaafe6be43bc154043f7175 (diff)
parent73fe77cf6761d70a964e02f1228ec6309579b6ae (diff)
downloadperlweeklychallenge-club-1036bfd73a5af401f2fb49d91cb22d62f9a782eb.tar.gz
perlweeklychallenge-club-1036bfd73a5af401f2fb49d91cb22d62f9a782eb.tar.bz2
perlweeklychallenge-club-1036bfd73a5af401f2fb49d91cb22d62f9a782eb.zip
Merge pull request #9619 from andemark/challenge-257
Challenge 257 Solutions (Raku)
-rw-r--r--challenge-257/mark-anderson/raku/ch-2.raku36
1 files changed, 23 insertions, 13 deletions
diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku
index a6f2d34b0c..5c4c4cf95d 100644
--- a/challenge-257/mark-anderson/raku/ch-2.raku
+++ b/challenge-257/mark-anderson/raku/ch-2.raku
@@ -37,12 +37,23 @@ nok reduced-row-echelon([1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]);
+nok reduced-row-echelon([1, 0, 0, 0],
+ [0, 1, 0, 1],
+ [0, 0, 1, 0],
+ [0, 0, 0, 1],
+ [0, 0, 0, 0]);
+
ok reduced-row-echelon([1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]);
+ok reduced-row-echelon([0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0]);
+
sub reduced-row-echelon(+@m)
{
# the first non-zero number in a row is the pivot
@@ -51,25 +62,24 @@ sub reduced-row-echelon(+@m)
# the first row that is all zeroes
my $k = @pivots.first(*.not, :k);
- # all 0 rows are grouped at the bottom
- if $k
+ # rows with all zeroes are grouped at the bottom
+ with $k
{
return False unless all(@pivots[$k..*]) eqv Any;
- @pivots = @pivots[^$k]
+ @pivots = @pivots[^$k];
+ return True unless @pivots
}
- my @keys = @pivots>>[0];
+ my @cols = @pivots>>[0];
@pivots = @pivots>>[1];
- # all pivots == 1
- return False unless all(@pivots) == 1;
-
# pivots go from top-left to bottom-right
- return False unless [<] @keys;
+ return False unless [<] @cols;
+
+ # remove extraneous rows and columns
+ @m = @m[^@pivots;@cols].batch(@cols.elems);
- # pivot columns are all 0 (except for the 1)
- return all (([Z] @m[^@pivots])[@keys]).map({
- all .sum == 1,
- all(.Bag.keys) == 0|1
- })
+ # @m should be an identity matrix at this point
+ # so check rows for all zeroes and a 1
+ return all @m.map({ all(.sum == 1, all(.Bag.keys) == 0|1) })
}