diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-20 13:06:27 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-20 13:06:27 +0000 |
| commit | 824bd441790d1b15a798dd76bc03cb2c8d51586a (patch) | |
| tree | 4fe8f8a1c6cfc9187a4696cb53114bfa10e12f19 | |
| parent | 436364fb272d0b24baff41d4125a30b37e451a53 (diff) | |
| parent | 30e58e28fbb0fa164bfd28f61d2c0cee568c983b (diff) | |
| download | perlweeklychallenge-club-824bd441790d1b15a798dd76bc03cb2c8d51586a.tar.gz perlweeklychallenge-club-824bd441790d1b15a798dd76bc03cb2c8d51586a.tar.bz2 perlweeklychallenge-club-824bd441790d1b15a798dd76bc03cb2c8d51586a.zip | |
Merge pull request #9605 from andemark/challenge-257
Initial 257 (Raku)
| -rw-r--r-- | challenge-257/mark-anderson/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-257/mark-anderson/raku/ch-2.raku | 75 |
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-257/mark-anderson/raku/ch-1.raku b/challenge-257/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..c3ef8325f0 --- /dev/null +++ b/challenge-257/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use Test; + +is-deeply smaller-than-current(5,2,1,6), (2,1,0,3); +is-deeply smaller-than-current(1,2,0,3), (1,2,0,3); +is-deeply smaller-than-current(0,1), (0,1); +is-deeply smaller-than-current(9,4,9,2), (2,1,2,0); + +sub smaller-than-current(*@a) +{ + @a.sort.squish.antipairs.Map{@a} +} diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..a6f2d34b0c --- /dev/null +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -0,0 +1,75 @@ +#!/usr/bin/env raku +use Test; + +ok reduced-row-echelon([1, 0, 0, 1], + [0, 1, 0, 2], + [0, 0, 1, 3]); + +nok reduced-row-echelon([1, 1, 0], + [0, 1, 0], + [0, 0, 0]); + +ok reduced-row-echelon([0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]); + +ok reduced-row-echelon([1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1]); + +nok reduced-row-echelon([0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0]); + +nok reduced-row-echelon([0, 1, 0], + [1, 0, 0], + [0, 0, 0]); + +nok reduced-row-echelon([4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1]); + +nok reduced-row-echelon([1, 0, 0, 0], + [0, 1, 0, 3], + [0, 0, 1,-3], + [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]); + +sub reduced-row-echelon(+@m) +{ + # the first non-zero number in a row is the pivot + my @pivots = @m>>.first(*.so, :kv); + + # the first row that is all zeroes + my $k = @pivots.first(*.not, :k); + + # all 0 rows are grouped at the bottom + if $k + { + return False unless all(@pivots[$k..*]) eqv Any; + @pivots = @pivots[^$k] + } + + my @keys = @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; + + # pivot columns are all 0 (except for the 1) + return all (([Z] @m[^@pivots])[@keys]).map({ + all .sum == 1, + all(.Bag.keys) == 0|1 + }) +} |
