diff options
Diffstat (limited to 'challenge-204/wlmb/perl')
| -rwxr-xr-x | challenge-204/wlmb/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-204/wlmb/perl/ch-2.pl | 22 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-204/wlmb/perl/ch-1.pl b/challenge-204/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..66cc937e71 --- /dev/null +++ b/challenge-204/wlmb/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# Perl weekly challenge 204 +# Task 1: Monotonic Array +# +# See https://wlmb.github.io/2023/02/13/PWC204/#task-1-monotonic-array +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to test if the sequence N1 N2... is monotonic + FIN +my @orig=@ARGV; +my $current=shift; +my ($increasing, $decreasing); +for(@ARGV){ + $_>$current and $increasing=1; + $_<$current and $decreasing=1; + last if $increasing and $decreasing; # shortcut if non monotonic + $current=$_; +} +my ($result, $reason)= + $increasing && $decreasing?(0, "Non-monotonic"): + $increasing ?(1, "Non-decreasing"): + $decreasing ?(1, "Non-increasing"): + (1, "Constant"); +say join " ", @orig, "->", $result, $reason; diff --git a/challenge-204/wlmb/perl/ch-2.pl b/challenge-204/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..af92096064 --- /dev/null +++ b/challenge-204/wlmb/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 204 +# Task 2: Reshape Matrix +# +# See https://wlmb.github.io/2023/02/13/PWC204/#task-2-reshape-matrix +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV==3; + Usage: $0 M r c + to convert matrix M (a string using PDL's notation) + to a matrix with r rows, c columns + FIN +my $M=pdl shift; +my ($rows, $cols)=@ARGV; +my $nelem=$M->nelem; # total number of elements +my $desired=$rows*$cols; +say("$M cannot be reshaped to $rows rows and $cols columns: 0"), exit + unless $desired==$nelem; +my $N=$M->copy; +$N->reshape($cols) if $rows==1; # 1D row vector +$N->reshape($cols, $rows) if $rows!=1; #2D matrix +say "$M as $rows x $cols matrix becomes $N" |
