diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2022-09-20 22:11:32 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2022-09-20 22:11:32 -0500 |
| commit | 7b1386ce6727a9d404b60d985a0c9db502ea1e39 (patch) | |
| tree | ef14bbd74081a73e95cedccc4e2a740b17c06838 | |
| parent | e66131f9e712f8da717bb0694dd5b67f06424666 (diff) | |
| download | perlweeklychallenge-club-7b1386ce6727a9d404b60d985a0c9db502ea1e39.tar.gz perlweeklychallenge-club-7b1386ce6727a9d404b60d985a0c9db502ea1e39.tar.bz2 perlweeklychallenge-club-7b1386ce6727a9d404b60d985a0c9db502ea1e39.zip | |
Solve PWC183
| -rw-r--r-- | challenge-183/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-183/wlmb/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-183/wlmb/perl/ch-2.pl | 31 |
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"; + } +} |
