diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-02-13 22:15:57 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-02-17 16:37:56 +0100 |
| commit | ccd952c897a0f7f1172d75b899b4fc253eb1b9a6 (patch) | |
| tree | 2c172a2c7b55cf1bc27c02a8d556b9ac5972c426 | |
| parent | 01ffdd66c1614f56cd977e20626479793adcb9b7 (diff) | |
| download | perlweeklychallenge-club-ccd952c897a0f7f1172d75b899b4fc253eb1b9a6.tar.gz perlweeklychallenge-club-ccd952c897a0f7f1172d75b899b4fc253eb1b9a6.tar.bz2 perlweeklychallenge-club-ccd952c897a0f7f1172d75b899b4fc253eb1b9a6.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-204/jo-37/perl/ch-2.pl | 73 |
1 files changed, 73 insertions, 0 deletions
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; +} |
