diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-23 12:03:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-23 12:03:29 +0000 |
| commit | fe05aaca6a64e8c4b461320fd4a544b668de171b (patch) | |
| tree | 59e8f3ba7a4ccb40061d95b7dc463cec140d5f54 | |
| parent | 29b1af3483563eac26835c6c5a6c98c343b1c8a5 (diff) | |
| parent | fd73abc34742f832a5b563278987a8ced203913e (diff) | |
| download | perlweeklychallenge-club-fe05aaca6a64e8c4b461320fd4a544b668de171b.tar.gz perlweeklychallenge-club-fe05aaca6a64e8c4b461320fd4a544b668de171b.tar.bz2 perlweeklychallenge-club-fe05aaca6a64e8c4b461320fd4a544b668de171b.zip | |
Merge pull request #9449 from choroba/ech253
Add solutions to 253: Split Strings & Weakest Row by E. Choroba
| -rwxr-xr-x | challenge-253/e-choroba/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-253/e-choroba/perl/ch-2.pl | 80 |
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-253/e-choroba/perl/ch-1.pl b/challenge-253/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..abde8f46e2 --- /dev/null +++ b/challenge-253/e-choroba/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub split_strings($separator, @words) { + return [grep length, map split(/\Q$separator/), @words] +} + +use Test2::V0; +plan 2; + +is split_strings('.', 'one.two.three','four.five','six'), + ['one', 'two', 'three', 'four', 'five', 'six'], + 'Example 1'; + +is split_strings('$', '$perl$$', '$$raku$'), + ['perl', 'raku'], + 'Example 2'; diff --git a/challenge-253/e-choroba/perl/ch-2.pl b/challenge-253/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..2c3dd8fbab --- /dev/null +++ b/challenge-253/e-choroba/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ first }; + +sub weakest_row($matrix) { + return [ + map $_->[0], + sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } + map [$_, scalar grep $_, @{ $matrix->[$_] }], + 0 .. $#$matrix + ] +} + +sub weakest_row_grep($matrix) { + return [sort { + grep($_, @{ $matrix->[$a] }) <=> grep($_, @{ $matrix->[$b] }) + || $a <=> $b + } 0 .. $#$matrix] +} + +sub weakest_row_first_schwartzian($matrix) { + return [ + map $_->[0], + sort { $a->[1] <=> $b->[1] || $a->[0] <=> $b->[0] } + map { my $r = $_; + [$r, + (first { ! $matrix->[$r][$_] } 0 .. @{ $matrix->[$r] }) + ] # We're using @, not $#, to prevent undef for all ones. + } 0 .. $#$matrix + ] +} + +use Test2::V0; +plan 3 * 3; + +my $SIZE = 500; + +my $m = [map { my $o = int rand $SIZE; + [(1) x $o, (0) x ($SIZE - $o)] + } 1 .. $SIZE]; + +for my $f (\&weakest_row, + \&weakest_row_grep, + \&weakest_row_first_schwartzian, +) { + is $f->([ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ]), [2, 0, 3, 1, 4], 'Example 1'; + + is $f->([ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ]), [0, 2, 3, 1], 'Example 2'; + + is $f->($m), weakest_row_grep($m), 'same'; +} + +use Benchmark qw{ cmpthese }; + +cmpthese(-5, { + schwartzian => sub { weakest_row($m) }, + grep => sub { weakest_row_grep($m) }, + first_schwartz => sub { weakest_row_first_schwartzian($m) } +}); + + +__END__ + Rate grep first_schwartz schwartzian +grep 9.22/s -- -89% -93% +first_schwartz 84.3/s 814% -- -34% +schwartzian 128/s 1284% 51% -- |
