aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-07-31 11:20:15 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-07-31 11:20:15 -0600
commit6ae2664bf24eb719a58990e5485274c7609f201f (patch)
treeca40f52a2586c11573a603325de0441626980120
parente511966ce2280dbedb2c916d9e6254708800639e (diff)
downloadperlweeklychallenge-club-6ae2664bf24eb719a58990e5485274c7609f201f.tar.gz
perlweeklychallenge-club-6ae2664bf24eb719a58990e5485274c7609f201f.tar.bz2
perlweeklychallenge-club-6ae2664bf24eb719a58990e5485274c7609f201f.zip
SOlve PWC228
-rw-r--r--challenge-228/wlmb/blog.txt1
-rwxr-xr-xchallenge-228/wlmb/perl/ch-1.pl14
-rwxr-xr-xchallenge-228/wlmb/perl/ch-2.pl24
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";