From 4b1b1f5752512f2daa23a575a3b3415858d86822 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 12:04:50 +0000 Subject: Initial 257 (Raku) --- challenge-257/mark-anderson/raku/ch-1.raku | 12 +++++++ challenge-257/mark-anderson/raku/ch-2.raku | 55 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 challenge-257/mark-anderson/raku/ch-1.raku create mode 100644 challenge-257/mark-anderson/raku/ch-2.raku 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..3bd8f8f5c4 --- /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.unique.antipairs.Hash{@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..fdca8d6a20 --- /dev/null +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +use Test; + +ok echelon([1,0,0,1], + [0,1,0,2], + [0,0,1,3]); + +nok echelon([1, 1, 0], + [0, 1, 0], + [0, 0, 0]); + +ok echelon([0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]); + +ok echelon([1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1]); + +nok echelon([0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0]); + +nok echelon([0, 1, 0], + [1, 0, 0], + [0, 0, 0]); + +nok echelon([4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1]); + +sub echelon(+@m) +{ + my @first = @m>>.first(*.so, :kv); + my $k = @first.first(*.so.not, :k); + + # if rows are all 0 then they are grouped at the bottom + if $k { return False unless so @first[$k..*].all eqv (Any) } + + my @non-zeroes = quietly @first[^$k] || @first; + + # first non-zero number in rows (without all zeroes) is a 1 + return False unless so @non-zeroes>>.[1].all == 1; + + # 1 is to the right of the 1 in the row above + return False unless [<] @non-zeroes>>[0]; + + # leading zero cols are all 0 (except for the 1) + return all @non-zeroes>>[0].map({ + all @m[*;$_].sum == 1, + all(@m[*;$_].Bag.keys) ~~ 0..1 + }) +} -- cgit From 761b080524abe43b72d10b9a4857f71de54769a9 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:28:29 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-1.raku | 2 +- challenge-257/mark-anderson/raku/ch-2.raku | 74 ++++++++++++++++-------------- challenge-257/mark-anderson/raku/tst.raku | 3 ++ 3 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 challenge-257/mark-anderson/raku/tst.raku diff --git a/challenge-257/mark-anderson/raku/ch-1.raku b/challenge-257/mark-anderson/raku/ch-1.raku index 3bd8f8f5c4..d70e068abc 100644 --- a/challenge-257/mark-anderson/raku/ch-1.raku +++ b/challenge-257/mark-anderson/raku/ch-1.raku @@ -8,5 +8,5 @@ is-deeply smaller-than-current(9,4,9,2), (2,1,2,0); sub smaller-than-current(*@a) { - @a.sort.unique.antipairs.Hash{@a} + @a.sort.unique.antipairs.Map{@a} } diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index fdca8d6a20..5492722dcc 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -1,54 +1,58 @@ #!/usr/bin/env raku use Test; -ok echelon([1,0,0,1], - [0,1,0,2], - [0,0,1,3]); +ok reduced-row-echelon([1, 0, 0, 1], + [0, 1, 0, 2], + [0, 0, 1, 3]); -nok echelon([1, 1, 0], - [0, 1, 0], - [0, 0, 0]); +nok reduced-row-echelon([1, 1, 0], + [0, 1, 0], + [0, 0, 0]); -ok 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([0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]); -ok echelon([1, 0, 0, 4], - [0, 1, 0, 7], - [0, 0, 1,-1]); +ok reduced-row-echelon([1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1]); -nok 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,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0]); -nok echelon([0, 1, 0], - [1, 0, 0], - [0, 0, 0]); +nok reduced-row-echelon([0, 1, 0], + [1, 0, 0], + [0, 0, 0]); -nok echelon([4, 0, 0, 0], - [0, 1, 0, 7], - [0, 0, 1,-1]); +nok reduced-row-echelon([4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1]); -sub echelon(+@m) +sub reduced-row-echelon(+@m) { - my @first = @m>>.first(*.so, :kv); - my $k = @first.first(*.so.not, :k); + # the first non-zero number in a row is the pivot + my @pivots = @m>>.first(*.so, :kv); - # if rows are all 0 then they are grouped at the bottom - if $k { return False unless so @first[$k..*].all eqv (Any) } + # find the first row that is all zeroes + my $k = @pivots.first(*.so.not, :k); - my @non-zeroes = quietly @first[^$k] || @first; + # all 0 rows are grouped at the bottom + if $k { + return False unless @pivots[$k..*].all eqv Any; + @pivots = @pivots[^$k] + } - # first non-zero number in rows (without all zeroes) is a 1 - return False unless so @non-zeroes>>.[1].all == 1; + # all pivots == 1 + return False unless so @pivots>>.[1].all == 1; - # 1 is to the right of the 1 in the row above - return False unless [<] @non-zeroes>>[0]; + # pivots go from top-left to bottom-right + return False unless [<] @pivots>>[0]; - # leading zero cols are all 0 (except for the 1) - return all @non-zeroes>>[0].map({ + # pivot columns are all 0 (except for the 1) + return all @pivots>>[0].map({ all @m[*;$_].sum == 1, all(@m[*;$_].Bag.keys) ~~ 0..1 }) diff --git a/challenge-257/mark-anderson/raku/tst.raku b/challenge-257/mark-anderson/raku/tst.raku new file mode 100644 index 0000000000..e111d1b0ee --- /dev/null +++ b/challenge-257/mark-anderson/raku/tst.raku @@ -0,0 +1,3 @@ +#!/usr/bin/env raku + +say (1..9).Array.splice(0); -- cgit From 16999d84c38058abf33d0341227f0cf38e84d21a Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:30:38 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/tst.raku | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 challenge-257/mark-anderson/raku/tst.raku diff --git a/challenge-257/mark-anderson/raku/tst.raku b/challenge-257/mark-anderson/raku/tst.raku deleted file mode 100644 index e111d1b0ee..0000000000 --- a/challenge-257/mark-anderson/raku/tst.raku +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env raku - -say (1..9).Array.splice(0); -- cgit From c0ed66161cb9372d01302da905ae15e7d3916a3c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:38:45 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 5492722dcc..1dc1835310 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -36,14 +36,15 @@ sub reduced-row-echelon(+@m) # the first non-zero number in a row is the pivot my @pivots = @m>>.first(*.so, :kv); - # find the first row that is all zeroes - my $k = @pivots.first(*.so.not, :k); + # 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 @pivots[$k..*].all eqv Any; - @pivots = @pivots[^$k] - } + if $k + { + return False unless @pivots[$k..*].all eqv Any; + @pivots = @pivots[^$k] + } # all pivots == 1 return False unless so @pivots>>.[1].all == 1; @@ -53,7 +54,7 @@ sub reduced-row-echelon(+@m) # pivot columns are all 0 (except for the 1) return all @pivots>>[0].map({ - all @m[*;$_].sum == 1, - all(@m[*;$_].Bag.keys) ~~ 0..1 - }) + all @m[*;$_].sum == 1, + all(@m[*;$_].Bag.keys) ~~ 0..1 + }) } -- cgit From c2158d09c24382fabf5876db85de423edafef65e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:44:17 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 1dc1835310..a601a9917f 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -47,7 +47,7 @@ sub reduced-row-echelon(+@m) } # all pivots == 1 - return False unless so @pivots>>.[1].all == 1; + return False unless so @pivots>>[1].all == 1; # pivots go from top-left to bottom-right return False unless [<] @pivots>>[0]; -- cgit From d6064d0d2afcf15717f9b6ee05a6b584f6a28c13 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 15:09:02 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-257/mark-anderson/raku/ch-1.raku b/challenge-257/mark-anderson/raku/ch-1.raku index d70e068abc..c3ef8325f0 100644 --- a/challenge-257/mark-anderson/raku/ch-1.raku +++ b/challenge-257/mark-anderson/raku/ch-1.raku @@ -8,5 +8,5 @@ is-deeply smaller-than-current(9,4,9,2), (2,1,2,0); sub smaller-than-current(*@a) { - @a.sort.unique.antipairs.Map{@a} + @a.sort.squish.antipairs.Map{@a} } -- cgit From 0e8472c2b3976fe2ab6d04866e62f2861fa5a558 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 16:11:10 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index a601a9917f..d727e72118 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -53,8 +53,5 @@ sub reduced-row-echelon(+@m) return False unless [<] @pivots>>[0]; # pivot columns are all 0 (except for the 1) - return all @pivots>>[0].map({ - all @m[*;$_].sum == 1, - all(@m[*;$_].Bag.keys) ~~ 0..1 - }) + return all @pivots>>[0].map({ @m[*;$_].sum == 1 }) } -- cgit From f427d52abe1564b1bbccabd37903fc9f0b47ba01 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 17:20:34 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index d727e72118..5d547b6316 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -53,5 +53,5 @@ sub reduced-row-echelon(+@m) return False unless [<] @pivots>>[0]; # pivot columns are all 0 (except for the 1) - return all @pivots>>[0].map({ @m[*;$_].sum == 1 }) + return all(([Z] @m)[@pivots>>[0]]>>.sum) == 1 } -- cgit From b3fed3f1c65f49ae58824e51be4275a2783940b0 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Feb 2024 17:52:00 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 5d547b6316..a801671767 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -53,5 +53,5 @@ sub reduced-row-echelon(+@m) return False unless [<] @pivots>>[0]; # pivot columns are all 0 (except for the 1) - return all(([Z] @m)[@pivots>>[0]]>>.sum) == 1 + return all(([Z] @m[^@pivots])[@pivots>>[0]]>>.sum) == 1 } -- cgit From f8e133d0e948b5aff8fb5fb037069dbe0eea3b1e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 20 Feb 2024 05:18:36 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index a801671767..6ec5d3307a 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -31,6 +31,12 @@ 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]); + sub reduced-row-echelon(+@m) { # the first non-zero number in a row is the pivot @@ -42,16 +48,19 @@ sub reduced-row-echelon(+@m) # all 0 rows are grouped at the bottom if $k { - return False unless @pivots[$k..*].all eqv Any; + return False unless all(@pivots[$k..*]) eqv Any; @pivots = @pivots[^$k] } # all pivots == 1 - return False unless so @pivots>>[1].all == 1; + return False unless all(@pivots>>[1]) == 1; # pivots go from top-left to bottom-right return False unless [<] @pivots>>[0]; # pivot columns are all 0 (except for the 1) - return all(([Z] @m[^@pivots])[@pivots>>[0]]>>.sum) == 1 + return all (([Z] @m[^@pivots])[@pivots>>[0]]).map({ + all .sum == 1, + all(.Bag.keys) ~~ 0..1 + }) } -- cgit From 59ca52f10ee43755c07cc65f75cfec0c60904b22 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 20 Feb 2024 10:10:59 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 6ec5d3307a..412ac4cf4b 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -37,10 +37,16 @@ nok reduced-row-echelon([1, 0, 0, 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]); + sub reduced-row-echelon(+@m) { # the first non-zero number in a row is the pivot - my @pivots = @m>>.first(*.so, :kv); + my @pivots = @m>>.first(*.so, :p); # the first row that is all zeroes my $k = @pivots.first(*.not, :k); @@ -52,15 +58,18 @@ sub reduced-row-echelon(+@m) @pivots = @pivots[^$k] } + my @keys = @pivots>>.keys>>[0]; + @pivots = @pivots>>.values>>[0]; + # all pivots == 1 - return False unless all(@pivots>>[1]) == 1; + return False unless all(@pivots) == 1; # pivots go from top-left to bottom-right - return False unless [<] @pivots>>[0]; + return False unless [<] @keys; # pivot columns are all 0 (except for the 1) - return all (([Z] @m[^@pivots])[@pivots>>[0]]).map({ - all .sum == 1, - all(.Bag.keys) ~~ 0..1 - }) + return all (([Z] @m[^@pivots])[@keys]).map({ + all .sum == 1, + all(.Bag.keys) ~~ 0..1 + }) } -- cgit From 70c36ac3ee4750d0b99f9600f9b2aae1a32e4d1c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 20 Feb 2024 10:23:02 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 412ac4cf4b..91000c85d5 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -46,7 +46,7 @@ ok reduced-row-echelon([1, 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, :p); + my @pivots = @m>>.first(*.so, :kv); # the first row that is all zeroes my $k = @pivots.first(*.not, :k); @@ -58,8 +58,8 @@ sub reduced-row-echelon(+@m) @pivots = @pivots[^$k] } - my @keys = @pivots>>.keys>>[0]; - @pivots = @pivots>>.values>>[0]; + my @keys = @pivots>>[0]; + @pivots = @pivots>>[1]; # all pivots == 1 return False unless all(@pivots) == 1; -- cgit From 30e58e28fbb0fa164bfd28f61d2c0cee568c983b Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:18:54 +0000 Subject: Challenge 257 Solutions (Raku) --- challenge-257/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index 91000c85d5..a6f2d34b0c 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -70,6 +70,6 @@ sub reduced-row-echelon(+@m) # pivot columns are all 0 (except for the 1) return all (([Z] @m[^@pivots])[@keys]).map({ all .sum == 1, - all(.Bag.keys) ~~ 0..1 + all(.Bag.keys) == 0|1 }) } -- cgit