aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-21 21:51:34 +0100
committerGitHub <noreply@github.com>2022-09-21 21:51:34 +0100
commite13d23ca88dd6b05c6b51ac8cc1e092cc78e1a6c (patch)
tree0296410ccbb07cf91ecb35384be5efb27652e3b4
parentb7ddf418051ef36114a133bdf71b56ce5e7a0c33 (diff)
parent7b1386ce6727a9d404b60d985a0c9db502ea1e39 (diff)
downloadperlweeklychallenge-club-e13d23ca88dd6b05c6b51ac8cc1e092cc78e1a6c.tar.gz
perlweeklychallenge-club-e13d23ca88dd6b05c6b51ac8cc1e092cc78e1a6c.tar.bz2
perlweeklychallenge-club-e13d23ca88dd6b05c6b51ac8cc1e092cc78e1a6c.zip
Merge pull request #6776 from wlmb/challenges
Solve PWC183
-rw-r--r--challenge-183/wlmb/blog.txt1
-rwxr-xr-xchallenge-183/wlmb/perl/ch-1.pl30
-rwxr-xr-xchallenge-183/wlmb/perl/ch-2.pl31
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-183/wlmb/blog.txt b/challenge-183/wlmb/blog.txt
new file mode 100644
index 0000000000..539ec7c597
--- /dev/null
+++ b/challenge-183/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2022/09/20/PWC183/
diff --git a/challenge-183/wlmb/perl/ch-1.pl b/challenge-183/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..d59c89a599
--- /dev/null
+++ b/challenge-183/wlmb/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 183
+# Task 1: Unique Array
+#
+# See https://wlmb.github.io/2022/09/20/PWC183/#task-1-unique-array
+use v5.36;
+use experimental qw(try);
+use PDL;
+use PDL::NiceSlice;
+die <<EOF
+Usage: $0 "[V1,V2...]" ...
+to print the unique arrays among V1, V2...
+Each V_i is an array [N1, N2...] of numbers N_j, or an array of arrays [W1,W2...] where
+each Wj is an array of...
+Admits multiple arguments.
+EOF
+unless @ARGV;
+for(@ARGV){
+ try {
+ my $input=pdl $_;
+ my $flat=$input->clump(-2);
+ my $repetitions=($flat->(:,:,*1)==$flat(:,*1,:))->andover;
+ my $redundant=($repetitions&($repetitions->xvals < $repetitions->yvals))->orover;
+ my $keep_indices=which(!$redundant);
+ say +$input->mv(-1,0)->index1d($keep_indices)->mv(0,-1);
+ }
+ catch ($m) {
+ say "\n$m\nInvalid ndarray: $_";
+ }
+}
diff --git a/challenge-183/wlmb/perl/ch-2.pl b/challenge-183/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..bee4bb629d
--- /dev/null
+++ b/challenge-183/wlmb/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 183
+# Task 2: Date Difference
+#
+# See https://wlmb.github.io/2022/09/20/PWC183/#task-2-date-difference
+use v5.36;
+use experimental qw(try for_list);
+use DateTime;
+use Date::Parse;
+die <<FIN unless @ARGV and @ARGV%2==0;
+Usage: $0 D1 D2 ...
+Print the date difference between dates D1 and D2, each in the format YYYY-MM-DD.
+Additional pairs of dates may be added;
+FIN
+
+for my ($date1, $date2)(@ARGV){
+ try {
+ my ($dt1, $dt2)=map {DateTime->from_epoch($_)} # ordered datetimes
+ sort {$a<=>$b}
+ map {str2time($_)//die "Wrong date: $_"} ($date1, $date2);
+ my $years=$dt2->subtract_datetime($dt1)->years; # year difference
+ $dt2->subtract(years=>$years); # reduce difference to within a year
+ my $days=$dt2->delta_days($dt1)->in_units("days"); # remaining days
+ my $years_name=$years==1?"year":"years"; # singular or plural
+ my $days_name=$days==1?"day":"days";
+ say "The difference between $date1 and $date2 is $years $years_name and $days $days_name";
+ }
+ catch($m){
+ say "Failed with $date1 and $date2\n$m";
+ }
+}