aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-271/wlmb/blog.txt1
-rwxr-xr-xchallenge-271/wlmb/perl/ch-1.pl19
-rwxr-xr-xchallenge-271/wlmb/perl/ch-2.pl17
3 files changed, 37 insertions, 0 deletions
diff --git a/challenge-271/wlmb/blog.txt b/challenge-271/wlmb/blog.txt
new file mode 100644
index 0000000000..fb39070635
--- /dev/null
+++ b/challenge-271/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2024/05/28/PWC271/
diff --git a/challenge-271/wlmb/perl/ch-1.pl b/challenge-271/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..692082a697
--- /dev/null
+++ b/challenge-271/wlmb/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 271
+# Task 1: Maximum Ones
+#
+# See https://wlmb.github.io/2024/05/28/PWC271/#task-1-maximum-ones
+use v5.36;
+use PDL;
+die <<~"FIN" unless @ARGV;
+ Usage: $0 [[m11 m12...][m21 m22...]...]
+ to find the row with the largest number of 1 entries,
+ or the first largest row in case of a tie.
+ Rows are numbered from 1 upwards.
+ FIN
+for(@ARGV){
+ my $in=pdl($_);
+ my @ones=($in==1)->sumover->dog; # ones in each row
+ my @sorted=sort {$ones[$b] <=> $ones[$a] || $a<=>$b} 0..@ones-1;
+ say "$in -> ",1+$sorted[0];
+}
diff --git a/challenge-271/wlmb/perl/ch-2.pl b/challenge-271/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..7fb7e827ae
--- /dev/null
+++ b/challenge-271/wlmb/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 271
+# Task 2: Sort by 1 bits
+#
+# See https://wlmb.github.io/2024/05/28/PWC271/#task-2-sort-by-1-bits
+use v5.36;
+die <<~"FIN" unless @ARGV;
+ Usage: $0 N1 N2...
+ to sort the numbers N1, N2... according to the number of 1 bits
+ and then according to value
+ FIN
+my @sorted = sort {ones($a) <=> ones($b) || $a<=>$b} @ARGV;
+say "@ARGV -> @sorted";
+
+sub ones($x){
+ 0+grep{$_}split "", sprintf "%b",$x;
+}