diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-02-17 16:38:47 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-02-17 16:38:47 +0100 |
| commit | f4176bc47c92ee191a4466d2aa9d5c5d89a49e32 (patch) | |
| tree | 2c172a2c7b55cf1bc27c02a8d556b9ac5972c426 | |
| parent | 6051a217ebf6f4804bf3015fc670971a489aecb0 (diff) | |
| parent | ccd952c897a0f7f1172d75b899b4fc253eb1b9a6 (diff) | |
| download | perlweeklychallenge-club-f4176bc47c92ee191a4466d2aa9d5c5d89a49e32.tar.gz perlweeklychallenge-club-f4176bc47c92ee191a4466d2aa9d5c5d89a49e32.tar.bz2 perlweeklychallenge-club-f4176bc47c92ee191a4466d2aa9d5c5d89a49e32.zip | |
Solutions to challenge 204
| -rw-r--r-- | challenge-203/jo-37/ch-202.md | 33 | ||||
| -rwxr-xr-x | challenge-203/jo-37/perl/ch-2.pl | 1 | ||||
| -rw-r--r-- | challenge-204/jo-37/ch-202.md | 33 | ||||
| -rwxr-xr-x | challenge-204/jo-37/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-204/jo-37/perl/ch-2.pl | 73 |
5 files changed, 136 insertions, 67 deletions
diff --git a/challenge-203/jo-37/ch-202.md b/challenge-203/jo-37/ch-202.md deleted file mode 100644 index d9e5420ee0..0000000000 --- a/challenge-203/jo-37/ch-202.md +++ /dev/null @@ -1,33 +0,0 @@ -# Sequential adjacent consecutive second-next neighbors - -**Preface:** -I wrote this text *after* I submitted my solution to the week 202's challenge *and* I looked throught all other solutions that were submitted so far. - -I was surprised to see other interpretations of task 1 that I hadn't thought about. To restate: - -> Write a script to print 1 if there are THREE consecutive odds in the given array otherwise print 0. - -Sounds clear: three consecutive odds. But wait: What is the meaning of "consecutive"? -I interpreted this as "three odd numbers consecutive in the list". Another valid interpretation would be: "three consecutive odd numbers contained in the list". Something completely different! - -An example for my interpretation: - - 2, 4, 9, 3, 7, 22 -has three consecutive odds: 9, 3, 7. - -On the other hand: - - 4, 9, 2, 11, 6, 7 -has three consecutive odds: 7, 9, 11. - -I refuse to interpret the task as: three consecutive consecutive odds, i.e. - - 2, 3, 5, 7, 8 - -regarding as valid but refusing - - 7, 2, 5, 8, 3 - -*and* - - 2, 7, 3, 5, 8 diff --git a/challenge-203/jo-37/perl/ch-2.pl b/challenge-203/jo-37/perl/ch-2.pl index 2d6b9eadfb..b7ffe49a31 100755 --- a/challenge-203/jo-37/perl/ch-2.pl +++ b/challenge-203/jo-37/perl/ch-2.pl @@ -45,7 +45,6 @@ copy_dirs(@ARGV); sub copy_dirs ($source, $target) { find { # Make sure the source path is not interpreted as a regex. - # # Make sure the source path is not interpreted as a regex. wanted => sub {-d && s/^\Q$source\E/$target/ && !-e && mkdir}, no_chdir => 1}, $source; } diff --git a/challenge-204/jo-37/ch-202.md b/challenge-204/jo-37/ch-202.md deleted file mode 100644 index d9e5420ee0..0000000000 --- a/challenge-204/jo-37/ch-202.md +++ /dev/null @@ -1,33 +0,0 @@ -# Sequential adjacent consecutive second-next neighbors - -**Preface:** -I wrote this text *after* I submitted my solution to the week 202's challenge *and* I looked throught all other solutions that were submitted so far. - -I was surprised to see other interpretations of task 1 that I hadn't thought about. To restate: - -> Write a script to print 1 if there are THREE consecutive odds in the given array otherwise print 0. - -Sounds clear: three consecutive odds. But wait: What is the meaning of "consecutive"? -I interpreted this as "three odd numbers consecutive in the list". Another valid interpretation would be: "three consecutive odd numbers contained in the list". Something completely different! - -An example for my interpretation: - - 2, 4, 9, 3, 7, 22 -has three consecutive odds: 9, 3, 7. - -On the other hand: - - 4, 9, 2, 11, 6, 7 -has three consecutive odds: 7, 9, 11. - -I refuse to interpret the task as: three consecutive consecutive odds, i.e. - - 2, 3, 5, 7, 8 - -regarding as valid but refusing - - 7, 2, 5, 8, 3 - -*and* - - 2, 7, 3, 5, 8 diff --git a/challenge-204/jo-37/perl/ch-1.pl b/challenge-204/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..6d9db6dac2 --- /dev/null +++ b/challenge-204/jo-37/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV > 1; +usage: $0 [-examples] [-tests] [--] [N1 N2...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N1 N2... + List of numbers + +EOS + + +### Input and Output + +say 0 + is_monotonic(@ARGV); + + +### Implementation + +# Comparing consecutive elements from the list yields values in the set +# {-1, 0, 1}, where a non-monotonic list has both -1 and 1 in the result +# set. Summating over the absolute values of the unique results from +# the comparisons thus reveals the monotonic property: The sum will be +# two for a non-monotonic list. +sub is_monotonic { + my $list = pdl @_; + + ($list(1:-1) <=> $list(0:-2))->uniq->abs->sum < 2; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok is_monotonic(1, 2, 2, 3), 'example 1'; + ok !is_monotonic(1, 3, 2), 'example 2'; + ok is_monotonic(6, 5, 5, 4), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-204/jo-37/perl/ch-2.pl b/challenge-204/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..8a550e738e --- /dev/null +++ b/challenge-204/jo-37/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use PDL; +use experimental 'signatures'; + +our ($tests, $examples, $rows, $cols); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV && $rows && $cols; +usage: $0 [-examples] [-tests] [-rows=R -cols=C MATRIX] + +-examples + run the examples from the challenge + +-tests + run some tests + +-rows=R +-cols=C + reshape the given matrix to RxC + +MATRIX + a matrix in any string form accepted by the pdl constructor, e.g. + '[1 2] [3 4] [5 6]' + +EOS + + +### Input and Output + +# Build an output string to be printed or use zero if it turns out to be +# empty. +say +(join "\n", map "[@$_]", @{reshape_matrix($rows, $cols, "@ARGV")}) || 0; + + +### Implementation + +# PDL's "reshape" is too forgiving for this task: It pads or truncates +# the data as needed. Returning a reshaped matrix only when the number +# of elements in both shapes match. +sub reshape_matrix ($r, $c, @matrix) { + my $m = long @matrix; + + $r * $c == nelem($m) ? $m->reshape($c, $r)->unpdl : []; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is reshape_matrix(1, 4, [1, 2], [3, 4]), + [[1, 2, 3, 4]], 'example 1'; + is reshape_matrix(3, 2, [1, 2, 3], [4, 5, 6]), + [[1, 2], [3, 4], [5, 6]], 'example 2'; + is reshape_matrix(3, 2, [1, 2]), [], 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is reshape_matrix(3, 10, sequence(5, 6)), sequence(10, 3)->unpdl, + '6x5 -> 3x10'; + } + + done_testing; + exit; +} |
