diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-26 14:46:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-26 14:46:57 +0000 |
| commit | df6315defa949965e442847206fd232a32f6938f (patch) | |
| tree | 378de12223f22a6230cdb4bf497e918a686a8d2c | |
| parent | 191f5f72dd22cf8872b8c4d32eaebf9773becb96 (diff) | |
| parent | aa97566f79c7d37417aa563f391d4b2b19094588 (diff) | |
| download | perlweeklychallenge-club-df6315defa949965e442847206fd232a32f6938f.tar.gz perlweeklychallenge-club-df6315defa949965e442847206fd232a32f6938f.tar.bz2 perlweeklychallenge-club-df6315defa949965e442847206fd232a32f6938f.zip | |
Merge pull request #9463 from jo-37/contrib
Solutions to challenge 253
| -rw-r--r-- | challenge-253/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-253/jo-37/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-253/jo-37/perl/ch-2.pl | 75 |
3 files changed, 138 insertions, 0 deletions
diff --git a/challenge-253/jo-37/blog.txt b/challenge-253/jo-37/blog.txt new file mode 100644 index 0000000000..906fab4611 --- /dev/null +++ b/challenge-253/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/blog/pwc/challenge-253 diff --git a/challenge-253/jo-37/perl/ch-1.pl b/challenge-253/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..70212982a6 --- /dev/null +++ b/challenge-253/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples, $sep); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless defined $sep && @ARGV; +usage: $0 [-examples] [-tests] [-sep=S STR...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-sep=C + use C as separator + +STR + list of strings + +EOS + + +### Input and Output + +say join ',', map qq("$_"), split_strings($sep, @ARGV); + + +### Implementation + +sub split_strings { + my $sep = shift; + grep length, map +(split /\Q$sep/), @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + 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'; + } + + SKIP: { + skip "tests" unless $tests; + + is [split_strings(0 => qw(00aa00b00 00c00dd00))], + [qw(aa b c dd)], 'empty fields with zero separator'; + } + + done_testing; + exit; +} diff --git a/challenge-253/jo-37/perl/ch-2.pl b/challenge-253/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..dad11ed26a --- /dev/null +++ b/challenge-253/jo-37/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [M] + +-examples + run the examples from the challenge + +-tests + run some tests + +M + a matrix in any form accepted by the PDL string constructor, + e.g. + "[[1, 2, 0, 0, 4], [1, 1, 1, 1, 0], [3, 0, 0, 0, 0]]" +EOS + + +### Input and Output + +say weakest("@ARGV"); + + +### Implementation + +sub weakest { + my $m = pdl @_; + $m->append($m((0))->ndcoords)->qsortveci; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is weakest([ + [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] + ])->unpdl, [2, 0, 3, 1, 4], 'example 1'; + + is weakest([ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ])->unpdl, [0, 2, 3, 1], 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is weakest([0, 1, 2, 3], + [1, 0, 0, 0], + [1, 2, 3, 4], + [0, 1, 2, 3], + [1, 2, 3, 5])->unpdl, [0, 3, 1, 2, 4], 'arbitrary matrix'; + } + + done_testing; + exit; +} |
