aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-22 00:48:59 +0100
committerGitHub <noreply@github.com>2023-05-22 00:48:59 +0100
commit3e3de745ad8755f75cdc987f61abcdf2017cc3e3 (patch)
tree9e73a9c8bca0943b1fb6c075113f7ea64f115e8c
parentfd4645856ef19f35ce7e43349a80053266044a28 (diff)
parent0df677500affd7e8aa41464e1aed224c63cd930a (diff)
downloadperlweeklychallenge-club-3e3de745ad8755f75cdc987f61abcdf2017cc3e3.tar.gz
perlweeklychallenge-club-3e3de745ad8755f75cdc987f61abcdf2017cc3e3.tar.bz2
perlweeklychallenge-club-3e3de745ad8755f75cdc987f61abcdf2017cc3e3.zip
Merge pull request #8120 from Util/c217
Add TWC 217 blog post and solutions by Bruce Gray (Perl and Raku).
-rw-r--r--challenge-217/bruce-gray/blog.txt1
-rw-r--r--challenge-217/bruce-gray/perl/ch-1.pl21
-rw-r--r--challenge-217/bruce-gray/perl/ch-2.pl32
-rw-r--r--challenge-217/bruce-gray/raku/ch-1.raku43
-rw-r--r--challenge-217/bruce-gray/raku/ch-2.raku34
5 files changed, 131 insertions, 0 deletions
diff --git a/challenge-217/bruce-gray/blog.txt b/challenge-217/bruce-gray/blog.txt
new file mode 100644
index 0000000000..3b036a082a
--- /dev/null
+++ b/challenge-217/bruce-gray/blog.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/bruce_gray/2023/05/twc-217-big-and-boundless-in-the-matrix.html
diff --git a/challenge-217/bruce-gray/perl/ch-1.pl b/challenge-217/bruce-gray/perl/ch-1.pl
new file mode 100644
index 0000000000..8ffe36e58b
--- /dev/null
+++ b/challenge-217/bruce-gray/perl/ch-1.pl
@@ -0,0 +1,21 @@
+use v5.36;
+
+sub task1 ( $m ) {
+ my ( $first, $second, $third, @rest )
+ = sort { $a <=> $b } map { $_->@* } @{$m};
+
+ return $third;
+}
+
+
+my @tests = (
+ [ 1, [ [3, 1, 2], [5, 2, 4], [0, 1, 3] ] ],
+ [ 4, [ [2, 1 ], [4, 5 ] ] ],
+ [ 0, [ [1, 0, 3], [0, 0, 0], [1, 2, 1] ] ],
+);
+use Test::More;
+plan tests => 0+@tests;
+for (@tests) {
+ my ( $expected, $in ) = @{$_};
+ is task1($in), $expected;
+}
diff --git a/challenge-217/bruce-gray/perl/ch-2.pl b/challenge-217/bruce-gray/perl/ch-2.pl
new file mode 100644
index 0000000000..5abe3af353
--- /dev/null
+++ b/challenge-217/bruce-gray/perl/ch-2.pl
@@ -0,0 +1,32 @@
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-217/#TASK2
+use v5.36;
+sub task2 ( @integers ) {
+ return join '', sort { "$b$a" <=> "$a$b" } @integers;
+}
+
+
+my @tests = (
+ [ 231, [1, 23] ],
+ [ 3210, [10, 3, 2] ],
+ [ 431210, [31, 2, 4, 10] ],
+ [ 542111, [5, 11, 4, 1, 2] ],
+ [ 110, [1, 10] ],
+
+ # Without this test case, padding-with-tilde-to-maximum-size looks like a viable solution.
+ [ 131, [1, 13] ],
+
+ # These tests break other pad-to-maximum-size algorithms.
+ [ 10100, [ 10, 100] ],
+ [ 10100, [ 100, 10] ],
+
+ # Input sizes that choke permutation-based algortihms.
+ [ 9908172635445362718, [ map 9 * $_, 1..10 ] ],
+ [ '999989796959493929190898888786858483828180797877776757473727170696867666656463626160595857565555453525150494847464544443424140393837363534333323130292827262524232222120191817161514131211110100', [ 1..100 ] ],
+);
+
+use Test::More;
+plan tests => 0+@tests;
+for (@tests) {
+ my ( $expected, $in ) = @{$_};
+ is task2(@{$in}), $expected;
+}
diff --git a/challenge-217/bruce-gray/raku/ch-1.raku b/challenge-217/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..fbec7e89bc
--- /dev/null
+++ b/challenge-217/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,43 @@
+sub task1 ( @m ) { @m[*;*].sort.[2] }
+
+sub task1_alternate ( @m ) {
+ my @top;
+ @top.append( .sort.head(3) ) for @m;
+ return @top.sort.head(3).tail;
+}
+
+
+my @tests =
+ ( 1, ( (3, 1, 2), (5, 2, 4), (0, 1, 3) ) ),
+ ( 4, ( (2, 1 ), (4, 5 ) ) ),
+ ( 0, ( (1, 0, 3), (0, 0, 0), (1, 2, 1) ) ),
+
+ # Alpha translations of the numeric tests above,
+ # because the generic sort supports it!
+ ( 'B', ( <D B C>, <F C E>, <A B D> ) ),
+ ( 'E', ( <C B >, <E F > ) ),
+ ( 'A', ( <B A D>, <A A A>, <B C B> ) ),
+;
+use Test;
+plan +@tests;
+for @tests -> ( $expected, @in ) {
+ is task1(@in), $expected;
+}
+
+{
+ my @ones = (1 xx 1000) xx 1000;
+ my ( $task1_time, $task1_alternate_time );
+ for ^10 {
+ {
+ my $t = now;
+ task1(@ones) == 1 or die for ^10;
+ $task1_time += now - $t;
+ }
+ {
+ my $t = now;
+ task1_alternate(@ones) == 1 or die for ^10;
+ $task1_alternate_time += now - $t;
+ }
+ }
+ say (:$task1_time, :$task1_alternate_time, x => $task1_time/$task1_alternate_time );
+}
diff --git a/challenge-217/bruce-gray/raku/ch-2.raku b/challenge-217/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..db8f35b220
--- /dev/null
+++ b/challenge-217/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,34 @@
+sub numerically_highest_concatenation ( $a, $b --> Order ) {
+ $b ~ $a <=> $a ~ $b
+}
+
+sub task2 ( @ns --> Str ) {
+ return @ns.sort(&numerically_highest_concatenation).join;
+}
+
+# See blog post for alternative approaches.
+
+
+my @tests =
+ ( 231, (1, 23) ),
+ ( 3210, (10, 3, 2) ),
+ ( 431210, (31, 2, 4, 10) ),
+ ( 542111, (5, 11, 4, 1, 2) ),
+ ( 110, (1, 10) ),
+
+ # Without this test case, padding-with-tilde-to-maximum-size looks like a viable solution.
+ ( 131, (1, 13) ),
+
+ # These tests break other pad-to-maximum-size algorithms.
+ ( 10100, ( 10, 100) ),
+ ( 10100, ( 100, 10) ),
+
+ # Input sizes that choke permutation-based algortihms.
+ ( 9908172635445362718, ( (1..10) X* 9 ) ),
+ ( 999989796959493929190898888786858483828180797877776757473727170696867666656463626160595857565555453525150494847464544443424140393837363534333323130292827262524232222120191817161514131211110100, ( 1..100 ) ),
+;
+use Test;
+plan +@tests;
+for @tests -> ( $expected, @in ) {
+ is task2(@in), $expected;
+}