From 0df677500affd7e8aa41464e1aed224c63cd930a Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 21 May 2023 15:16:23 -0500 Subject: Add TWC 217 blog post and solutions by Bruce Gray (Perl and Raku). --- challenge-217/bruce-gray/blog.txt | 1 + challenge-217/bruce-gray/perl/ch-1.pl | 21 ++++++++++++++++ challenge-217/bruce-gray/perl/ch-2.pl | 32 ++++++++++++++++++++++++ challenge-217/bruce-gray/raku/ch-1.raku | 43 +++++++++++++++++++++++++++++++++ challenge-217/bruce-gray/raku/ch-2.raku | 34 ++++++++++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 challenge-217/bruce-gray/blog.txt create mode 100644 challenge-217/bruce-gray/perl/ch-1.pl create mode 100644 challenge-217/bruce-gray/perl/ch-2.pl create mode 100644 challenge-217/bruce-gray/raku/ch-1.raku create mode 100644 challenge-217/bruce-gray/raku/ch-2.raku 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', ( , , ) ), + ( 'E', ( , ) ), + ( 'A', ( , , ) ), +; +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; +} -- cgit