diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-31 23:59:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-31 23:59:50 +0100 |
| commit | c2e7015872e2eab42f14b9df72c5c975aed5d1bf (patch) | |
| tree | 256bc5b11d4ce497d79bde1b6991502f20d418c2 | |
| parent | f2eb3c291a8a368a41b1afa007bb39f9e2e20ca1 (diff) | |
| parent | 6ae2664bf24eb719a58990e5485274c7609f201f (diff) | |
| download | perlweeklychallenge-club-c2e7015872e2eab42f14b9df72c5c975aed5d1bf.tar.gz perlweeklychallenge-club-c2e7015872e2eab42f14b9df72c5c975aed5d1bf.tar.bz2 perlweeklychallenge-club-c2e7015872e2eab42f14b9df72c5c975aed5d1bf.zip | |
Merge pull request #8481 from wlmb/challenges
Solve PWC228
| -rw-r--r-- | challenge-228/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-228/wlmb/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-228/wlmb/perl/ch-2.pl | 24 |
3 files changed, 39 insertions, 0 deletions
diff --git a/challenge-228/wlmb/blog.txt b/challenge-228/wlmb/blog.txt new file mode 100644 index 0000000000..be2a50f129 --- /dev/null +++ b/challenge-228/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/07/31/PWC228/ diff --git a/challenge-228/wlmb/perl/ch-1.pl b/challenge-228/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..fd5a773be1 --- /dev/null +++ b/challenge-228/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 228 +# Task 1: Unique Sum +# +# See https://wlmb.github.io/2023/07/31/PWC228/#task-1-unique-sum +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + Usage $0 N1 [N2... ] + to sum unique elements of the array (N1 N2...) + FIN +my %count; +++$count{$_} for @ARGV; +say "@ARGV -> ", sum0 grep {$count{$_}==1} @ARGV diff --git a/challenge-228/wlmb/perl/ch-2.pl b/challenge-228/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..214720e552 --- /dev/null +++ b/challenge-228/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 228 +# Task 2: Empty Array +# +# See https://wlmb.github.io/2023/07/31/PWC228/#task-2-empty-array +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV; + Usage $0 N1 [N2...] + to count how many operations are required to empty the + array (N1 N2...) + FIN +my $x=(my $in=pdl(@ARGV))->copy; +my $count=0; +while($x->nelem>1){ # while there are two or more elements + my $min=$x->min; + my $idx=which($x==$min)->at(0); # index of minimum element + $count+=$idx+1; # num. of ops. to empty minimum element + $x=$x->rotate(-$idx) # bring smallest element to the front, rotating to the + # end the initial larger elements + ->slice([1,-1,1]); # remove smallest element, keep the rest +} +++$count; # add the removal of the last element +say "$in -> $count"; |
