diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-02-15 11:51:39 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-02-15 11:51:39 -0500 |
| commit | 714bd7fa84dc5532ddc15cbe577669cdb3a70fae (patch) | |
| tree | 785a4db6399016186d50c0de7325297fa416fc87 /challenge-204/wlmb | |
| parent | 24d339929a25ed203e44ea5d46fcdcc552d2e139 (diff) | |
| parent | 6051a217ebf6f4804bf3015fc670971a489aecb0 (diff) | |
| download | perlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.tar.gz perlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.tar.bz2 perlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-204/wlmb')
| -rw-r--r-- | challenge-204/wlmb/blog.txt | 2 | ||||
| -rwxr-xr-x | challenge-204/wlmb/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-204/wlmb/perl/ch-2.pl | 22 |
3 files changed, 49 insertions, 0 deletions
diff --git a/challenge-204/wlmb/blog.txt b/challenge-204/wlmb/blog.txt new file mode 100644 index 0000000000..da9fde3295 --- /dev/null +++ b/challenge-204/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/02/13/PWC204/ + 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" |
